【发布时间】:2015-04-07 11:38:21
【问题描述】:
我有代码:
#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 的 IntelliSense: no operator "==" matches these operands 和编译器 (Visual 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==?
【问题讨论】:
-
请注意,一旦您修复了
operator==,您可以将IsCardInHand()改写为return std::find(hand.begin(), hand.end(), checkCard) != hand.end();
标签: c++ reference pass-by-reference