c++ - GMOCK 方法抛出编译错误

标签 c++ c++11 googlemock

当我尝试像下面那样实现 GMOCK 方法时出现编译错误,但是当我删除它时代码和 MOCK 类工作正常:

MOCK_METHOD2(myfunc, void (std::shared_ptr<route>, std::unique_ptr<message, std::default_delete<message> >));

我得到以下编译错误:

error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = message; _Dp = std::default_delete<message>]'
             MOCK_METHOD2(ship, void (std::shared_ptr<route>, std::unique_ptr<message, std::default_delete<message> >));
             ^
        In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/memory:81:0,
        ......................................................................................
        C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/unique_ptr.h:356:7: note: declared here
               unique_ptr(const unique_ptr&) = delete;
    error:   initializing argument 2 of 'R testing::internal::FunctionMocker<R(A1, A2)>::Invoke(A1, A2) [with R = void; A1 = std::shared_ptr<route>; A2 = std::unique_ptr<message>]'
       R Invoke(A1 a1, A2 a2) {
         ^
    In file included from ...

    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple: In instantiation of 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long long unsigned int _Idx = 1ull; _Head = std::unique_ptr<message>]':
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:255:44:   recursively required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long long unsigned int _Idx = 1ull; _Head = std::unique_ptr<message>; _Tail = {}]'
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:255:44:   required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long long unsigned int _Idx = 0ull; _Head = std::shared_ptr<cmb::mim::MimChannel>; _Tail = {std::unique_ptr<message, std::default_delete<message> >}]'
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:531:30:   required from 'constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = std::shared_ptr<cmb::mim::MimChannel>; _T2 = std::unique_ptr<message>]'
    C:/work/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:122:50:   required from 'R testing::internal::FunctionMocker<R(A1, A2)>::Invoke(A1, A2) [with R = void; A1 = std::shared_ptr<cmb::mim::MimChannel>; A2 = std::unique_ptr<message>]'
    C:/work/Mock.hpp:39:5:   required from here
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:134:25: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = message; _Dp = std::default_delete<message>]'
           : _M_head_impl(__h) { }

我不确定错误是什么以及如何修复它 - 评论或删除编译正常的 MOCK 方法声明?

最佳答案

我能够通过如下所述操作函数来解决问题:

MOCK_METHOD2(myfuncproxy, void (std::shared_ptr<route>, message&));

void myfunc((std::shared_ptr<route> r, std::unique_ptr<message, std::default_delete<message> > m)
{
    myfuncproxy(r,*m.get());
}

关于c++ - GMOCK 方法抛出编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47565900/

相关文章:

c++ - Gmock 匹配器匹配类型

c++ - C++中包含的C标准库函数是否抛出异常?

c++ - 类模板的内部类的类元组结构化绑定(bind)

c++ - 为什么 std::declval 添加引用?

c++ - move 语义和对象类型参数

c++ - 试图对两个编译时元组整数序列求和

c++ - 如何根据对象的某些字段在 C++ 中的对象 vector 中获取最小或最大元素?

c++ - OpenGL4 : Rotation looks all wrong

c++ - 使用谷歌模拟模拟转换运算符 int()

c++ - 在设置阶段忽略模拟调用