我一直在做这个项目,我非常接近完成,但有一个问题是函数超载 - 任何提示都会令人惊叹!这是我的代码:
#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;
}
}
}
我得到的错误是:
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'
任何帮助都会很棒!谢谢xx