gpt4 book ai didi

c++ - GoogleMock 是否让我在模拟类中实现析构函数?

转载 作者:行者123 更新时间:2023-11-28 05:43:39 26 4
gpt4 key购买 nike

我在运行 make 时不断收到这些错误:

debug/main.o: In function `MockMQAdapter<SomeClass>::MockMQAdapter()':
/source/Tests/testsfoo/MockMQAdapter.h:30: undefined reference to `MQAdapter<SomeClass>::~MQAdapter()'
debug/main.o:(.rodata._ZTVN2TW9MQAdapterI6ThingyEE[_ZTVN2TW9MQAdapterI6ThingyEE]+0x10): undefined reference to MQAdapter<SomeClass>::~MQAdapter()'
debug/main.o:(.rodata._ZTVN2TW9MQAdapterI6ThingyEE[_ZTVN2TW9MQAdapterI6ThingyEE]+0x18): undefined reference to `MQAdapter<SomeClass>::~MQAdapter()'
debug/main.o:(.rodata._ZTVN2TW9MQAdapterI6ThingyEE[_ZTVN2TW9MQAdapterI6ThingyEE]+0x20): undefined reference to `MQAdapter<SomeClass>::publish(std::string const&, std::string &message)'

这是我的代码:

#include <gmock/gmock.h>

template<typename S>
class MQAdapter {
public:
MQAdapter(const std::string host, uint16_t port);
virtual ~MQAdapter();
virtual void publish(const std::string queue, std::string message);
};

MQAdapter::MQAdapter(const std::string host, uint16_t port) {}

//Generated by gmock_gen.py
template <typename T0>
class MockMQAdapter : public MQAdapter<T0> {
public:
MOCK_METHOD2_T(publish,
void(std::string, std::string));
};

我非常仔细地遵循了谷歌模拟指南。我不明白这些错误是什么意思。这是我的测试结果:

TEST(MyTest, ExpectCall) {
MockMQAdapter<SomeClass> *adapter = new MockMQAdapter<SomeClass>("host", 1);
EXPECT_CALL(*adapter, publish("hi", "hello"));
adapter->publish("hi", "hello");
}

最佳答案

您已声明 MQAdapter 析构函数,但未定义它。因此,链接器在尝试解决它时会提示。提供一个定义,default 就足够了,即 virtual ~MQAdapter() = default;

另一方面,构造函数的定义应该是内联的或者用模板参数限定:

template <typename S>
MQAdapter<S>::MQAdapter(const std::string host, uint16_t port) {}

我猜这是因为这只是一个示例,但您没有对任何内容使用 MQAdapter 模板参数,因此它可能是一个常规类。

关于c++ - GoogleMock 是否让我在模拟类中实现析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36656661/

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