2015-04-07 34 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