c++ - 无法从类型 'Class::operator==' bool 转换为 'bool (Class::)(Class*) to type '

标签 c++ class operator-overloading equality-operator

<分区>

我正在做一种方法来查看两张牌是否具有相同的数字或花色

这是我的重载运算符和我调用重载运算符的方法

bool Carta::operator==(Carta *c){
   if(getNumCarta()==c->getNumCarta() || getNumPal()==c->getNumPal()){
        return true;
    }
    else{
        return false;
    }
    //return(numCarta==c->numCarta)||(numPal!==c->numPal);
}

bool Carta::operator!=(Carta *c){
    if(getNumCarta()!=c->getNumCarta() && getNumPal()!=c->getNumPal()){
        return true;
    }
    else{
        return false;
    }
}

bool Table::pairs(){

    for(int i=1;i<posFull;i++){

        if(t[i-1]->operator == t[i+1]){
            return true;
        }
    }
    return false;
}

我希望返回 true 但从未发生

最佳答案

看来你的意思是

if(t[i-1]->operator ==( t[i+1]) ){

或者你可以这样写

if( *t[i-1] == t[i+1] ){

但是这个运算符声明

bool operator==(Carta *c);

只会让代码的读者感到困惑。声明运算符会更好

bool operator ==( const Carta &c ) const;

像这样使用它

if( *t[i-1] == *t[i+1] ){

关于c++ - 无法从类型 'Class::operator==' bool 转换为 'bool (Class::)(Class*) to type ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375164/

相关文章:

c++ - 数据成员的编译时重新安排?

c++ - 如何设置使用 CreateProcess 创建的进程的名称?

c++ - 如何在 IMFByteStream 接口(interface)中实现 `BeginRead`

jquery - 使用 jQuery 添加类不显示效果

c++ - 将类链接到多个其他类

Ruby =~ 与 === 运算符

c++ - 将一个类的方法指针存储到另一个类中

c++ - 使用 googlemock 时出现 SEH 异常

c++ - 加法运算符重载不起作用

c++ - 澄清智能指针的 operator* 和 operator-> 重载