2012-03-06 31 views
0

文字列配列をソートするには、次の演算子をオーバーロードしようとしています。私はすべての関数を1つのクラスに持っていますが、 "この演算子関数のパラメータが多すぎます"というエラーが発生しています。実際には、1つのパラメータしか受け入れません。私は問題を調べ、フォーラムでは、クラス内で演算子をオーバーロードするときに1つのパラメータしか使用できないと言っていました。これは私にはあまり意味がありません。私は文字列を比較しようとしているので、オーバーロードのために2つのパラメータが必要です。私はクラス外の演算子に負担をかけることになっていますが、これはどのように機能しますか?バイナリ演算子C++オーバーロード

ここに私のコードです:

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

class Preprocessing 
{ 

public: 

void readFile(string list[], int size); 
void quickSort(int list[], int lowerBound, int upperBound); 
void swapItem(int &a, int &b); 

//These are the overloading functions I'm trying to implement 
bool operator<=(string a, string b); 
bool operator<(string a, string b); 
bool operator>(string a, string b); 
}; 

void Preprocessing::readFile(string list[], int size) 
{ 
ifstream myFile; 
myFile.open("words.txt"); 

for (int i = 0; i < size; i++) 
{ 
    myFile >> list[i]; 
} 

myFile.close(); 
} 

void Preprocessing::quickSort(int list[], int lowerBound, int upperBound) 
{ 
    int i, j, pivot; 

    i = lowerBound; 
    j = upperBound; 

    pivot = list[(i + j)/2]; 

    while (i <= j) 
    { 
     while(list[i] < pivot) 
     { 
      i = i + 1; 
     } 
     while (list[j] > pivot) 
     { 
      j = j - 1; 
     } 
     if (i <= j) 
     { 
      swapItem(list[i], list[j]); 
      i = i + 1; 
      j = j - 1; 
     }//end if 
    }//end outter while 
    if (lowerBound < j) 
    { 
     quickSort(list, lowerBound, j); 
    } 
    if (i < upperBound) 
    { 
     quickSort(list, i, upperBound); 
    }//end recursive if 
}//end function 

void Preprocessing::swapItem(int &a, int &b){ 
    int tmp; 

    tmp = a; 
    a = b; 
    b = tmp; 
} 

bool Preprocessing::operator<=(string a, string b) 
{ 
if (a.compare(b) > 0) 
    return false; 
else if (a.compare(b) == 0) 
    return true; 
else 
    return true; 
} 

bool Preprocessing::operator<(string a, string b) 
{ 
if (a.compare(b) > 0) 
    return false; 
else if (a.compare(b) == 0) 
    return true; 
else 
    return true; 
} 

bool Preprocessing::operator>(string a, string b) 
{ 
if (a.compare(b) > 0) 
    return false; 
else if (a.compare(b) == 0) 
    return true; 
else 
    return true; 
} 
+0

'std :: string'を比較するための標準演算子を置き換えようとしていますか?あるいは、あなたの型を 'std :: string'に匹敵させようとしていますか? –

答えて

5

事業者のための署名が正しくない:

bool operator<=(string a, string b); 
bool operator<(string a, string b); 
bool operator>(string a, string b); 
  1. あなたは演算子をオーバーロードするとき - あなたはメンバ関数として実装、それが唯一のはず1つの引数を受け入れる(他ののものと比較すると
  2. メンバー以外の関数2つの引数を指定することはできますが、既存の演算子(既にstd::stringに定義されているもの)と一致することはできません。通常は、テストのためにクラスをlhsとrhsとして受け入れる必要があります。
+0

Oh、o.k.だから最初のパラメータを宣言することは冗長でしょうか? – rocklandcitizen

+0

@rocklandcitizen - あなたはそれを持っています。 – prelic

+1

@rocklandcitizenは冗長ではなく、不要です。左側のオペランド(「第1のパラメータ」)はクラスのインスタンスで、 'this'ポインタとしてアクセス可能です。 – bonsaiviking

0

左辺がthisポインタとして渡されるため、引数が1つしかありません。

+0

ありがとうございます。それは魅力のように働く。 – rocklandcitizen

+0

@rocklandcitizen - それはあなたのためにうまくいってうれしい! – prelic

0

オペレータに過負荷をかけるには、オペレータが左オペランドのメソッドである必要があります。 C++は、引数(オペランド)の型に基づいて関数(および演算子)を選択します。クラス内では、左オペランドはクラスのインスタンスであり、thisポインタとして使用できるため、右オペランドだけが演算子の引数として指定できます。あなたの例では

、あなたがこれを行うことができます:文字列にPreprocessingオブジェクトを比較するための<=オペレータを定義し

class Preprocessing { 
    public: 
     bool operator<=(string b); 
}; 

。文字列比較演算子をオーバーロードする必要がある場合は、std::stringクラスを変更する必要があります。これは私の知る限りではありません。

1

operatorは、それが何であれ、その演算子をそのクラスのインスタンスに、またオプションでパラメータに適用する特別な意味を持ちます。

例ではoperator<=Preprocessingクラスのインスタンスをstringと比較することになっています。

class Preprocessing 
{ 
public: 
    bool operator<=(string a); 

private: 
    string aStringField; 
} 

は、通常は、パラメータを指定してインスタンスを比較するために、オペレータメソッド本体の内部thisを使用します。

bool Preprocessing::operator<=(string a) 
{ 
    return this->aStringField.length() <= a.length(); 
} 

そして、あなたがそれを呼び出す:

Preprocessing p; 
if (p <= "a string") 
    // ... 

に相当します

Preprocessing p; 
if (p.operator<=("a string")) 
    // ... 

「ポイント構文」を呼び出す必要のない演算子を提供する場合は、クラス外に存在する演算子friendが必要です。

class Preprocessing 
    { 
    public: 
     friend ostream& operator<<(ostream&, const Preprocessing&); 

    private: 
     string aStringField; 
    } 
関連する問題