我想要等于2个对象,确切地说是卡片(单元测试和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();
}我有错误:
状态错误C2678二进制“==”:没有找到任何操作符,它接受'const‘类型的左操作数(或者没有可接受的转换)
我尝试了重载操作符‘=’,但这不起作用:/也许,我必须走另一条路吗?这是我的第一个单元测试:D。
发布于 2016-12-30 21:08:39
相等操作符需要是const
bool operator==(const Card &card) const {
^^^^^有关const方法的讨论,请参见Meaning of "const" last in a C++ method declaration?
发布于 2016-12-30 21:12:55
试试这个:
bool operator ==(const Card& card) {
return returnValue() == card.returnValue();
}我想你只是把&放错地方了。
https://stackoverflow.com/questions/41402634
复制相似问题