-3

私はこのプロジェクトに取り組んでおり、完成に非常に近づいていますが、機能がオーバーロードされているという問題が 1 つあります。これが私のコードです:

#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

1 つの署名で関数を宣言しました。たとえば、次のようになります。

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 に答える