gpt4 book ai didi

c++ - 如何使用签名 `object ()` 模拟函数

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

我想用声明 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 的定义,很难分辨。听起来它试图从临时构造 B 但失败了,因为它无法将临时引用绑定(bind)到非常量引用。

例如,您的复制构造函数可能定义为:

class A {
public:
class B {
public:
// This should be const, without good reason to make it otherwise.
B(B&);
};
};

通过修复,仅使其成为常量引用。

关于c++ - 如何使用签名 `object ()` 模拟函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3702731/

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