文字列配列をソートするには、次の演算子をオーバーロードしようとしています。私はすべての関数を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;
}
'std :: string'を比較するための標準演算子を置き換えようとしていますか?あるいは、あなたの型を 'std :: string'に匹敵させようとしていますか? –