我有
class Conatact{
.....
bool operator<(Contact &c);
};
bool operator<(Contact &c)
{
return this.getName<c.getName();
}
它说“bool operator<(Contact&)”必须恰好接受两个参数 当我尝试将其更改为有两个参数时
bool operator<(Contact &c)
{
return this.getName<c.getName();
}
它说它必须恰好接受一个参数
最佳答案
我认为您需要通过提供完全限定名称向编译器表明它是一个成员实现:
bool Conatact::operator<(Contact &c)
{
return this->getName() < c.getName();
}
最好让你的操作符const
,同时让Contact &c
const
。
如果没有范围解析限定符,编译器会认为您正在定义一个“独立”运算符来比较联系人,在这种情况下,该运算符确实需要采用两个参数:
bool operator<(const Contact &lhs, const Contact &rhs) {
...
}
关于c++ - `bool operator<(Contact&)' 必须正好有两个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16107736/