使用C ++面向对象的代码重载问题

时间:2013-02-02 14:47:27

标签: c++

我一直在研究这个项目而且我已经非常接近完成了但是有一个问题就是说它们超载的功能 - 任何提示都会很棒!这是我的代码:

#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

5 个答案:

答案 0 :(得分:2)

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

void Enter();

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

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

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

虽然我不明白你为什么需要函数参数来开始。这些函数看起来像是要处理它们被调用的OpAmps对象。那么为什么要将OpAmps作为一个参数呢?您确定不打算使用this对象吗? this是指向调用成员函数的对象的指针。例如:

cin >> this->Name;

答案 1 :(得分: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 &);
};

答案 2 :(得分: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;

答案 3 :(得分:0)

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

void Enter();

Enter(OpAmps &, unsigned long &);

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

答案 4 :(得分: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);