I have been working on this project and I am very close to complete but have one issue with functions saying they are overloaded – any hints would be amazing! Here is my code:
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <string>
using namespace std;
class OpAmps
{
private:
string Name;
unsigned int PinCount;
double SlewRate;
public:
void Enter();
void Save();
void Load();
void Sort();
void Display();
friend bool SortName(const OpAmps &, const OpAmps &);
friend bool SortSlewRate(const OpAmps &, const OpAmps &);
};
#define DATABASE_MAX 10
#define DATABASE_FILENAME "database.txt"
int main()
{
OpAmps OpAmp[DATABASE_MAX];
OpAmps Menu;
unsigned long database_length = 0;
char UserInput;
while (1)
{
cout << endl;
cout << "Op-amp database menu" << endl;
cout << "--------------------" << endl;
cout << "1. Enter a new op-amp into the database" << endl;
cout << "2. Save the database to disk" << endl;
cout << "3. Load the database from disk" << endl;
cout << "4. Sort the database" << endl;
cout << "5. Display the database" << endl;
cout << "6. Exit from the program" << endl << endl;
cout << "Enter your option: ";
cin >> UserInput;
cout << endl;
switch(UserInput)
{
case '1':
Menu.Enter();
break;
case '2':
Menu.Save();
break;
case '3':
Menu.Load();
break;
case '4':
Menu.Sort();
break;
case '5':
Menu.Display();
break;
case '6':
return 0;
default:
cout << "Invalid entry" << endl << endl;
break;
}
}
}
void OpAmps::Enter(OpAmps& Op, unsigned long& database_length)
{
if (database_length == DATABASE_MAX)
{
cout << "The database is full" << endl;
}
else
{
cout << "Add new data" << endl;
cout << "------------" << endl;
cout << "Enter op-amp name: ";
cin >> Op.Name;
cout << "Enter number of pins: ";
cin >> Op.PinCount;
cout << "Enter slew rate: ";
cin >> Op.SlewRate;
cout << endl;
database_length++;
}
}
void OpAmps::Save(const OpAmps* Op, unsigned long database_length)
{
fstream output_file;
output_file.open(DATABASE_FILENAME, ios::out);
if(output_file.good())
{
output_file << database_length << endl << endl;
for (unsigned long i=0;i<database_length;i++)
{
output_file << Op[i].Name << endl;
output_file << Op[i].PinCount << endl;
output_file << Op[i].SlewRate << endl << endl;
}
}
output_file.close();
}
void OpAmps::Load(OpAmps* Op, unsigned long& database_length)
{
fstream input_file;
input_file.open(DATABASE_FILENAME, ios::in);
if(input_file.good())
{
input_file >> database_length;
for (unsigned long i=0;i<database_length;i++)
{
input_file >> Op[i].Name;
input_file >> Op[i].PinCount;
input_file >> Op[i].SlewRate;
}
}
input_file.close();
}
void OpAmps::Sort(OpAmps* Op, unsigned long database_length)
{
char UserInput;
cout << endl;
cout << "Sorting options" << endl;
cout << "---------------" << endl;
cout << "1. To sort by name" << endl;
cout << "2. To sort by slew rate" << endl;
cout << "3. No sorting" << endl << endl;
cout << "Enter your option: ";
cin >> UserInput;
cout << endl;
switch(UserInput)
{
case '1':
cout<<"sortName"<<endl;
std::sort(Op, Op + database_length, SortName);
break;
case '2':
cout<<"sortslew"<<endl;
std::sort(Op,Op + database_length, SortSlewRate);
break;
case '3':
return;
default:
cout << "Invalid entry" << endl << endl;
break;
}
}
bool SortName(const OpAmps &First, const OpAmps &Second)
{
return First.Name < Second.Name;
}
bool SortSlewRate (const OpAmps &First, const OpAmps &Second)
{
return First.SlewRate < Second.SlewRate;
}
void OpAmps::Display(const OpAmps* Op, unsigned long database_length)
{
if (database_length == 0)
{
cout << "No elements in the database" << endl;
}
else
{
cout << endl;
for (unsigned long i=0;i<database_length;i++)
{
cout << "Name: " << Op[i].Name <<endl;
cout << "Number of Pins: " << Op[i].PinCount << endl;
cout << "Slew Rate: " << Op[i].SlewRate << endl;
cout << endl;
}
}
}
The errors I am getting are:
error C2511: 'void OpAmps::Enter(OpAmps &,unsigned long &)' : overloaded member function not found in 'OpAmps'
see declaration of 'OpAmps'
error C2511: 'void OpAmps::Save(const OpAmps *,unsigned long)' : overloaded member function not found in 'OpAmps'
see declaration of 'OpAmps'
error C2511: 'void OpAmps::Load(OpAmps *,unsigned long &)' : overloaded member function not found in 'OpAmps'
see declaration of 'OpAmps'
error C2511: 'void OpAmps::Sort(OpAmps *,unsigned long)' : overloaded member function not found in 'OpAmps'
see declaration of 'OpAmps'
error C2511: 'void OpAmps::Display(const OpAmps *,unsigned long)' : overloaded member function not found in 'OpAmps'
see declaration of 'OpAmps'
Any help would be amazing!!
Thanks xx
You prototype this function:
And then you implemented it as:
Which doesn’t make sense in the slightest.
Either change the signature, or the implementation, and I think that in this case you’d be better off changing the signature.
But even then, I don’t think the first
OpAmps¶meter in the implementation is necessary. You should usethis. So change the method prototype into:And the implementation into:
And change any use of the unnecessary
opparameter with thethiskeyword. The same holds for anyOpAmps::?function you’re not prototyping/implementing correctly.The reason for this is because the functions you’re declaring are non-static member functions, meaning they always belong to an instance. You don’t need to pass the instance to the method like you would in C, instead you can use the instance the method is called on to with the
thiskeyword. Thethiskeyword is a constant pointer to the instance the method is called on (in your case that would be a pointer toMenu).You could therefore use:
Instead of:
However, in this case, it’s unnecessary to use the
thiskeyword. The C++ will figure out itself whether the field/function is instance-bound or not: