2016-12-30 68 views
1

我想等於2個對象,完全是卡片(單元測試用gtest)。 這是我的代碼:Gtest,等於對象

#include "stdafx.h" 
#include <gtest\gtest.h> 
#include <vector> 

class Card { 
public: 
    Card(int value, int color) :value(value), color(color) {}; 
    int returnColor() const { return color; }; 
    int returnValue() const { return value; }; 
    bool operator==(const Card &card) { 
     return returnValue() == card.returnValue(); 
    }; 
private: 
    int value; 
    int color; 
}; 

class CardTest : public ::testing::Test { 
protected: 
    std::vector<Card> cards; 
    CardTest() { cards.push_back(Card(10, 2)); 
    cards.push_back(Card(10, 3)); 
    }; 
}; 
TEST_F(CardTest, firstTest) 
{ 
    EXPECT_EQ(cards.at(0), cards.at(1)); 
} 
int main(int argc, char *argv[]) 
{ 
    testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

我有錯誤:

State Error C2678 binary '==': no operator found which takes a left-hand operand of type 'const Card' (or there is no acceptable conversion)

我嘗試超負荷運營商 '==',但是這不工作:/ 也許,我必須去其他辦法嗎? 這是我的第一次單元測試:D。

+0

gtest.h中的錯誤點行1448 – 21koizyd

+1

'bool operator ==(const Card&card)const {...'?換句話說,函數不僅應該保證不改變它給出的引用,它還應該保證不會改變this。 – Unimportant

回答

-1

試試這個:

bool operator ==(const Card& card) { 
    return returnValue() == card.returnValue(); 
} 

我認爲你只是有&在錯誤的地方。

+0

我很抱歉,但這是完全錯誤的。 – NPE

+0

是的,我嘗試這和我的解決方案一樣 – 21koizyd