2016-12-30 105 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