gpt4 book ai didi

c++ - Gtest,等于对象

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:26 25 4
gpt4 key购买 nike

我想要等于 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();
}

我有错误:

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?

关于c++ - Gtest,等于对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41402634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com