빠른 정렬 또는 가능한 병합 정렬 알고리즘을 사용하여 문자열 배열을 정렬하려면 다음 연산자를 오버로드하려고합니다. 단일 클래스에서 모든 함수가 있지만 "이 연산자 함수에 대해 너무 많은 매개 변수"오류가 발생합니다. 사실 하나의 매개 변수 만 허용합니다. 문제를 찾아보고 포럼에서 누군가가 클래스 내부에서 연산자를 오버로드 할 때 매개 변수 하나만 사용할 수 있다고 말했습니다. 이것은 나에게별로 의미가 없습니다. 오버로드를 위해 두 개의 매개 변수가 필요하므로 문자열을 비교하려고합니다. 수업 외부의 운영자에게 과부하가 걸릴 수 있습니까? 어떻게 작동합니까? 운영자에 대한이진 연산자 오버로드 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'과 비교하려고합니까? –