2012-03-06 74 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; 
    }