比较运算符我的代码:正确写引用
#include <vector>
#define DECK_SIZE 52
struct Card {
int Value;
int Suit;
bool operator == (Card &c1) {
return ((Value == c1.Value) && (Suit == c1.Suit));
}
};
typedef std::vector<Card> hand_t;
bool IsCardInHand(const Card & checkCard, const hand_t & hand)
{
for (int i = 0; i < static_cast<int>(hand.size()); ++i) {
if (checkCard == hand[i]) {
return true;
}
}
return false;
}
这条线:if (checkCard == hand[i])
产生一个错误:智能感知的IntelliSense: no operator "==" matches these operands
和编译器(视觉C++ 2010)的error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Card' (or there is no acceptable conversion)
。
请帮忙,我该如何正确地重写operator==
?
注意,一旦你解决'运营商==',你可以重写'IsCardInHand()'来只是'返回的std ::发现(hand.begin (),hand.end(),checkCard)!= hand.end();' – Barry