Gtest,等于对象
问题描述:
我想等于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。
答
等于运算符必须是const
:
bool operator==(const Card &card) const {
^^^^^
对于const的方法的讨论,请参见 Meaning of "const" last in a C++ method declaration?
+0
谢谢,我知道,但第一次我必须使用运算符与const。这很简单... – 21koizyd
gtest.h中的错误点行1448 – 21koizyd
'bool operator ==(const Card&card)const {...'?换句话说,函数不仅应该保证不改变它给出的引用,它还应该保证不会改变this。 – Unimportant