2012-03-06 76 views
0

我想重载以下运算符使用快速排序或可能合并排序算法排序字符串数组。我有我的所有功能在一个类,但我得到“这个运算符功能太多参数”错误。的确,它只会接受一个参数。我查了一下这个问题,并在一个论坛上有人说,你只能在一个班级里面重载一个操作符时使用一个参数。这对我来说没有多大意义。我试图比较字符串,所以我需要重载的两个参数。我是否应该让班级以外的操作员负担过重,这将如何工作?二进制运算符重载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'相媲美? – 2012-03-06 17:18:10

回答

5

对于运营商的签名是不正确的:

bool operator<=(string a, string b); 
bool operator<(string a, string b); 
bool operator>(string a, string b); 
  1. 当重载操作员 - 你实现它作为一个成员函数,它仅应接受一个参数(其他东西比较)
  2. 如果非成员函数(即friend),th你可以提供两个参数,但它不能匹配一个退出的操作符(已经为std::string定义了一个),并且通常应该接受你的类作为lhs和rhs进行测试。
+0

哦,哦。那么声明第一个参数会是多余的呢? – rocklandcitizen 2012-03-06 17:13:10

+0

@rocklandcitizen - 你明白了。 – prelic 2012-03-06 17:15:08

+1

@rocklandcitizen不是多余的,不必要的。左边的操作数(“第一个参数”)是你的类的一个实例,可以用'this'指针访问。 – bonsaiviking 2012-03-06 17:15:08

0

它只需要一个参数,因为左手边是作为this指针传递的。

+0

非常感谢。它像一个魅力。 – rocklandcitizen 2012-03-06 18:53:06

+0

@rocklandcitizen - 很高兴为你工作! – prelic 2012-03-06 20:31:28

0

要重载一个操作符,操作符需要是左操作数的一个方法。 C++根据参数的类型(操作数)选择函数(和运算符)。在一个类中,左边的操作数是该类的一个实例,可作为this指针使用,因此只有右手操作数可以被指定为操作符的参数。

在您的例子,你可以这样做:

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

这将定义一个<=运营商Preprocessing对象比较字符串。如果您需要重载字符串比较运算符,则需要修改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; 
    }