没有匹配'operator =='C ++的编译错误 [英] No match for 'operator==' C++ compile error

查看:86
本文介绍了没有匹配'operator =='C ++的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++新手提出的另一个问题。

Another question from a C++ newbie.

对于以下代码块,我收到编译器错误与'operator =='不匹配

I'm receiving a compiler error "No match for 'operator=='" for the following block of code

void swap(Team t1, Player p1, Team t2, Player p2){
    Player new_t1[11];
    Player new_t2[11];
    for(int i=0; i<11; i++){
        new_t1[i] = t1.get_player(i);
        new_t2[i] = t2.get_player(i);
        if(new_t1[i] == p1){
            new_t1[i] = p2;
        }
        if(new_t2[i] == p2){
            new_t2[i] = p1;
        }
    }

    cout << "Players swapped.";
}

有什么想法吗?

推荐答案

编译器不知道这两个播放器相同是什么意思。如果名称相同,它们是否相同?还是他们的ID?您需要为 class Player 定义==运算符。

The compiler doesn't know what it means for the two players to be the same. Are they the same if their names are the same? Or their IDs? You need to define == operator for class Player.

bool operator == (const Player &p1, const Player &p2)
{
   if( / * evaluate their equality */)
     return true;
   else
     return false;
}

此外,我不认为您的交换( )函数现在生效。您可能需要更改它以接受 Team s和 Player s作为参考。

Also, I don't think your swap() function has any effect right now. You may want to change it to accept Teams and Players by reference.

这篇关于没有匹配'operator =='C ++的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆