比較運算符我的代碼:正確寫引用
#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