如何使用签名`object()``如何模拟函数''
-
02-10-2019 - |
题
我想用声明嘲笑一种方法 A::B X(void)
. 。定义如下。
class A {
class B;
virtual B X() = 0;
};
class A::B {
public:
auto_ptr<int> something;
};
遵循此的模拟课是非常标准的。
class mA : public A
{
public:
MOCK_METHOD0(X, A::B());
};
但是,编译了这给我这个怪异的错误,我无法追踪它。这怎么了?
In member function ‘virtual A::B mA::X()’:
...: error: no matching function for call to ‘A::B::B(A::B)’
...: note: candidates are: A::B::B()
...: A::B::B(A::B&)
更新 我找到了一个失败的代码示例来证明这一点。
#include <gmock/gmock.h>
#include <memory>
using std::auto_ptr;
class thing {
public:
class result;
virtual result accessor () = 0;
};
class thing::result {
auto_ptr<int> x; // If this just "int", error goes away.
};
namespace mock {
class thing : ::thing {
public:
MOCK_METHOD0 ( accessor, result() );
};
}
解决方案
如果没有A和B的定义,就很难说出它正在试图从临时和失败中构造A B,因为它无法将临时性绑定到非const参考。
例如,您的复制构造函数可能被定义为:
class A {
public:
class B {
public:
// This should be const, without good reason to make it otherwise.
B(B&);
};
};
修复程序仅使其成为const参考。
不隶属于 StackOverflow