2015-04-07 38 views
0

比较运算符我的代码:正确写引用

#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==

+2

注意,一旦你解决'运营商==',你可以重写'IsCardInHand()'来只是'返回的std ::发现(hand.begin (),hand.end(),checkCard)!= hand.end();' – Barry

回答

4

你需要(注意consts):

bool operator == (const Card &c1) const { 
+0

谢谢:-)很明显,我的漏头:( – vladon

+1

嗯,这是早上在我的世界的一部分,所以你可以有那是你的借口:) – tweej