2014-09-25 57 views
4
#include <iostream> 
using namespace std; 

class family 
{ 
private: 
     double weight; 
     double height; 
public: 
     family(double x,double y); 
     ~family(); 
     double getWeight(); 
     double getHeight(); 
     double setWeight(); 
     double setHeight(); 
     bool operator==(const family &,const family &); 
}; 

bool family::operator ==(const family &a,const family &b) 
{ 
     return(a.getWeight() == b.getWeight()); 
}  

family::family(double x, double y) 
{ 
     weight = x; 
     height = y; 
} 

double family::getWeight() 
{ 
     return weight; 
} 

double family::getHeight() 
{ 
     return height; 
} 

family::~family(){} 

int main() 
{ 
    family a(70.0,175.2); 
    family b(68.5,178.2); 

    if(a==b) 
    cout << "A is bigger than B" << endl; 
    else 
    cout << "A is smaller than B" << endl; 

return 0; 
} 

我想使用重載等於運算符的方法。 不過,我有一個錯誤信息'a == b'中的'operator =='不匹配

"no match for ‘operator ==’ in ‘a == b’" 

爲什麼此錯誤消息來呢?此外,我想知道爲什麼在(const系列&,常量系列&)中有參考符號「&」。 請給我一些建議,修改我的代碼b.b.

回答

4

爲什麼會出現此錯誤消息?

如果你實現一個二元運算符作爲成員函數,它只接收右側作爲參數,左側是調用對象。如果你寫:

a == b 

的編譯器會滿足任何一個功能:

(return type) (type of lhs)::operator==((type of rhs)); 

(return type) operator==((type of lhs), (type of rhs)); 

注:(返回類型)可以是任何東西,但通常你」我想在這裏返回布爾。它不影響編譯器在進行函數調用時查找的內容。

你的函數簽名的卻是:

(return type) (type: family)::operator==((type: family), (type: family)); 

這是期待參數(一個暗示的)!


此外,我想知道爲什麼會出現在(const的家庭&,常量家庭&)參考符號 「&」。

const family &是該函數接受的參數的類型。它通過引用接收類型爲family的對象(即它使用原始對象而不是複製它們),並承諾不會修改它們(const)。編譯器將執行這個承諾。由於函數不需要修改任何一個對象,也沒有理由完成任何一個對象的完整副本,所以這正是使用的正確簽名。對於非成員函數。

對於一個成員函數,你需要稍微修改:

class family 
{ // ... 
    bool operator==(
    // ... 
} 

這是好的,到目前爲止,我們沒有改變任何東西。您的參數列表應該只包含右側參數,因此:

bool operator==(const family&) 

但是我們沒有完成。請記住非成員函數如何使用「const family &」作爲參數類型?不知何故,我們需要將調用對象標記爲const。我們通過在最後添加常量做到這一點:(調用對象已經可以如同通過引用)

bool operator==(const family&) const; 


當你去寫函數本身,只需使用:

bool family::operator==(const family &rhs) const { 
    ... 
} 

然後函數的身體,你可以直接使用調用對象的成員和RHS,或撥打他們的相關功能,比如:

return weight == rhs.weight; // direct member access 

return getWeight() == rhs.getWeight(); // using functions 
2

如果您將operator==作爲成員函數實現,則只需要一個參數。

還是在實踐中,你可以實現它作爲一個免費的功能

class family 
{ 
private: 
     double weight; 
     double height; 
public: 
     family(double x,double y); 
     ~family(); 
     double getWeight() const; 
     double getHeight() const; 
     double setWeight(); 
     double setHeight(); 
}; 

bool operator==(const family &a,const family &b) 
{ 
    return a.getWeight() == b.getWeight(); 
} 

更新: AS operator==需要常量家庭對象,你需要做getWight()/getHeight()功能常量。

1

你可以做的代碼以下更改:

double getWeight() const; 

。 。

bool operator==(const family &); 

。 。

bool family::operator==(const family &b) 
{ 
     return weight == b.getWeight(); 
} 
0

兩種可能性: (1)如果你想保持你的定義相同: 聲明爲非成員和朋友:

friend bool operator==(const family &,const family &); 

定義爲:

bool operator ==(const family &a,const family &b) 
{ 
     return(a.getWeight() == b.getWeight()); 
} 

( 2)聲明爲一個成員(隱含的參數是當前指向的對象): 聲明爲非成員和朋友:

bool operator==(const family &); 

定義爲:

bool family::operator ==(const family &a) 
{ 
     return(this->getWeight() == a.getWeight()); 
}