#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.