-3

我一直在做这个项目,我非常接近完成,但有一个问题是函数超载 - 任何提示都会令人惊叹!这是我的代码:

#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

4

5 回答 5

2

您使用一个签名声明了函数,例如:

void Enter();

但后来你用另一个实现了它们:

void OpAmps::Enter(OpAmps& Op, unsigned long& database_length)

函数签名必须在声明和定义之间匹配。您可以实现多个名称相同但签名不同的函数,这称为“重载”,但在这种情况下,您提供的重载是您从不声明的,并声明您从未实现的函数。

虽然我不明白为什么你需要函数参数开始。这些函数看起来像是要在调用它们的 OpAmps 对象上工作。那么为什么要传递运算放大器作为参数呢?您确定您不是要处理该this对象吗?this是指向调用成员函数的对象的指针。例如:

cin >> this->Name;
于 2013-02-02T14:51:27.910 回答
1

您将函数声明为不带参数:

void Enter();
void Save();
void Load();
void Sort();
void Display();

但是然后您将它们定义为接受参数:

void OpAmps::Enter(OpAmps& Op, unsigned long& database_length)
{
     ...
}

void OpAmps::Save(const OpAmps* Op, unsigned long database_length)
{
     ...
}

等等。编译器无法将这些定义与任何声明相匹配,因此它会发出这些错误消息。

要解决此问题,请按如下方式更改您的类定义:

class OpAmps
{
private:
    string Name;
    unsigned int PinCount;
    double SlewRate;
public:
    void Enter(OpAmps& Op, unsigned long& database_length);
    void Save(OpAmps const* Op, unsigned long& database_length);
    void Load(OpAmps* Op, unsigned long& database_length);
    void Sort(OpAmps* Op, unsigned long& database_length);
    void Display(OpAmps const* Op, unsigned long& database_length);

    friend bool SortName(const OpAmps &, const OpAmps &);
    friend bool SortSlewRate(const OpAmps &, const OpAmps &);
};
于 2013-02-02T14:51:52.777 回答
1

你原型这个函数:

void Enter();

然后你将它实现为:

void OpAmps::Enter(OpAmps& Op, unsigned long& database_length)

这一点都没有意义。

要么更改签名,要么更改实现,我认为在这种情况下你最好更改签名。

但即便如此,我认为OpAmps&实现中的第一个参数是不必要的。你应该使用this. 所以把方法原型改成:

void Enter(unsigned long&);

并实施成:

void OpAmps::Enter(unsigned long& database_length)

并使用关键字更改对不必要op参数的任何使用。this对于OpAmps::?您没有正确原型化/实现的任何功能也是如此。

这样做的原因是因为您声明的函数是非静态成员函数,这意味着它们始终属于一个实例。您不需要像在 C 中那样将实例传递给方法,而是可以使用this关键字调用该方法的实例。this关键字是指向调用该方法的实例的常量指针(在您的情况下,这将是指向 的指针)Menu

因此,您可以使用:

cin >> this->Name;

代替:

cin >> op.Name;

但是,在这种情况下,没有必要使用this关键字。C++ 将自行判断字段/函数是否是实例绑定的:

cin >> Name;
于 2013-02-02T14:52:46.037 回答
0

函数类声明的签名与实现版本的签名不同。它们必须相同。所以改变:

void Enter();

Enter(OpAmps &, unsigned long &);

以及您执行此操作的所有其他实例。

于 2013-02-02T14:51:37.853 回答
0

用以下内容替换您的方法原型:

void Enter(OpAmps& Op, unsigned long& database_length);
  void Save(OpAmps& Op, unsigned long& database_length);
  void Load(OpAmps& Op, unsigned long& database_length);
  void Sort(OpAmps& Op, unsigned long& database_length);
  void Display(OpAmps& Op, unsigned long& database_length);
于 2013-02-02T14:54:28.743 回答