c++ - 错误:如果(object1 == object2)没有匹配'operator =='(操作数类型是'const class'和'const class')
问题描述
我的代码有一些错误,我不知道如何解决。错误是:
错误:'operator==' 不匹配(操作数类型为 'const Secuencia' 和 const Secuencia') 如果(sec1 == sec2) 注意:候选人:'bool Secuencia::operator==(const Secuencia&)' 布尔运算符 == (const Secuencia & otro); 注意:将 'const Secuencia*' 作为 'this' 参数传递会丢弃限定符
在我的 .h 中,我有:
bool operator == (const Secuencia & other);
在我的 .cpp 中:
bool Secuencia :: operator == (const Secuencia & other){
bool same = (used == other.used && capacity == other.capacity ? true:false);
if(same == true){
for(int i = 0; i < used && same == true; i++){
if(info[i] != otro.info[i])
same = false;
}
}
return (same);
}
解决方案
如果你定义operator ==
为一个成员函数,它应该是const
:
bool operator == (const Secuencia & other) const;