2014-09-25 53 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()); 
}