gmock在mock_method上编译错误(在testing::internal::FunctionMocker中)
gmock compilation error (within testing::internal::FunctionMocker) on mock_method
当我尝试模拟一个函数时,我得到了一些奇怪的编译错误。编译器报错了复制构造函数。
代码片段:
class db_key {
public:
db_key(void) {}
explicit db_key(const char* buf) {}
~db_key(void) {}
};
class bar_A {
public:
explicit bar_A(const db_key& key): m_key(key) {}
virtual ~bar_A(void) {}
const db_key& dbkey(void) const { return m_key; }
private:
const db_key m_key;
};
class bar_B: bar_A {
public:
explicit bar_B(const db_key& key): bar_A(key) {}
virtual int read(int index) const { return index; }
};
class accessor_c {
public:
static const char* name(void) { return "general_accessor"; }
static accessor_c instance(const char* _db_name)
{
return accessor_c(_db_name);
}
virtual ~accessor_c(void) {}
const bar_B mem;
virtual const bar_B& get_bar_B_mem(void) const
{
return mem;
}
protected:
explicit accessor_c(const char* _db_name):
mem(db_key("foo")), m_db_name(_db_name) {}
private:
const char* const m_db_name;
};
inline accessor_c accessor(const char* db_name)
{
return accessor_c::instance(db_name);
}
模拟:
class mock_accessor_c : public accessor_c {
public:
explicit mock_accessor_c(const char* _db_name):
accessor_c(_db_name) {}
virtual ~accessor_c(void) {}
static mock_accessor_c instance(const char* _db_name)
{
return mock_accessor_c(_db_name);
}
MOCK_CONST_METHOD0(get_bar_B_mem, const bar_B&(void));
};
int main(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
我没有设法破译我得到的错误信息…
In file included from /usr/include/gtest/internal/gtest-internal.h:40:0,
from /usr/include/gtest/gtest.h:57,
from mock.cpp:1:
/usr/include/gmock/gmock-spec-builders.h: In copy constructor ‘testing::internal::FunctionMocker<const bar_B&()>::FunctionMocker(const testing::internal::FunctionMocker<const bar_B&()>&)’:
/usr/include/gmock/gmock-spec-builders.h:1728:3: error: ‘testing::internal::FunctionMockerBase<F>::FunctionMockerBase(const testing::internal::FunctionMockerBase<F>&) [with F = const bar_B&()]’ is private
GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase);
^
In file included from /usr/include/gmock/gmock.h:61:0,
from mock.cpp:2:
/usr/include/gmock/gmock-generated-function-mockers.h:61:7: error: within this context
class FunctionMocker<R()> : public
^
mock.cpp: In copy constructor ‘mock_accessor_c::mock_accessor_c(const mock_accessor_c&)’:
mock.cpp:55:7: note: synthesized method ‘testing::internal::FunctionMocker<const bar_B&()>::FunctionMocker(const testing::internal::FunctionMocker<const bar_B&()>&)’ first required here
class mock_accessor_c : public accessor_c {
^
mock.cpp: In static member function ‘static mock_accessor_c mock_accessor_c::instance(const char*)’:
mock.cpp:62:37: note: synthesized method ‘mock_accessor_c::mock_accessor_c(const mock_accessor_c&)’ first required here
return mock_accessor_c(_db_name);
^
简短的回答是模拟对象不可复制,因此编译器会对私有构造函数产生错误。根据对代码的快速视觉检查,mock_accessor_c::instance()方法导致了这个错误,因为它返回了一个按值模拟,从而生成了一个副本。
我建议完全放弃mock_accessor_c::instance()方法,而是在需要时直接构造mock。
相关文章:
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误
- 谷歌测试编译错误 Os X:函数式转换从"int"到"internal::FloatingEq2Matcher"的模糊转换<float>
- 如何解决g++问题"internal compiler error: Illegal instruction min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MI
- C++构造函数具有内部链接,但未定义 [-Wundefined-internal]
- 为什么日食总是发生错误"An internal error occurred during: "通知选择侦听器". java.lang.StackOverflowError"
- gtest - 未定义对"testing::InitGoogleTest(int*, char**)"的引用
- Lambda`s internal this in c++
- 使用libmosquitto的客户端使用TLS获得"tlsv1 alert internal error",没有TLS即可正常工作
- 命名空间 v8 和命名空间 v8::internal 有什么区别?
- gtest 与 Conan 一起安装:未定义对“testing::internal::GetBoolAssertionF
- 加载共享库时未定义的符号"tbb internal Allocate"
- 错误"<url> is not recognized as an internal or external command, operable program or batch file.
- I/O流操纵器-AndingField Internal -C vsC
- 对Google :: ProtoBuf :: Internal :: empty_string_ [abi:cxx11]
- 在windbg中"!heap -h"输出中"Internal"的含义是什么?
- C++Protobuf错误google::Protobuf::internal::kEmptyString错误
- GCC "Internal compiler error: Error reporting routines re-entered"(包括完整代码)
- 有人熟悉"implementation/internal header"(*.ih)吗?
- g肥皂 EWS "Error 500: Internal Server Error"
- gmock在mock_method上编译错误(在testing::internal::FunctionMocker中)