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

C++ 如何使用签名`object()模拟函数`,c++,mocking,googlemock,C++,Mocking,Googlemock,我想用声明a::bx(void)模拟一个方法。定义如下 class A { class B; virtual B X() = 0; }; class A::B { public: auto_ptr<int> something; }; 然而,这给了我一个奇怪的错误,我一直无法找到它。这有什么问题 In member function ‘virtual A::B mA::X()’: ...: error: no matching function for

我想用声明
a::bx(void)
模拟一个方法。定义如下

class A {
    class B;
    virtual B X() = 0;
};

class A::B {
  public:
    auto_ptr<int> something;
};
然而,这给了我一个奇怪的错误,我一直无法找到它。这有什么问题

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() );
    };
}
#包括
#包括
使用std::auto_ptr;
阶级事务{
公众:
班级成绩;
虚拟结果访问器()=0;
};
类事物::结果{
auto_ptr x;//如果这只是“int”,错误就会消失。
};
名称空间模拟{
类事物:::类事物{
公众:
MOCK_METHOD0(访问器,result());
};
}

如果没有A和B的定义,很难判断。听起来好像它试图从临时变量构造B,但失败了,因为它无法将临时变量绑定到非常量引用

例如,复制构造函数可以定义为:

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

由于修复只是将其作为常量参考。

MOCK_方法0
a
B
X
的定义是…?
MOCK_方法0
是由Google MOCK给出的,在这里:我无法在较小的代码片段上重现这一点,尽管它们遵循Google MOCK的所有正常模式。我在这里画出a,B和X的草图。啊!我刚刚发现,如果我去掉一个公共的auto_ptr数据成员,错误就会消失。嗯,我没有定义复制构造函数,但是可能。。。这是否会受到该类内部成员的影响<例如,AutoTypRT,绝对的,AutoTypTR有一个非const拷贝构造函数,它迫使B有一个非const拷贝构造函数。作为一个修复,您应该考虑在复制的情况下您想用那个指针做什么;如果要复制以在类之间共享访问权限,请使用共享指针(可能只有在指向的对象是不可变的情况下才有效),或者编写复制构造函数来克隆对象(如果它们都应该有自己的对象),或者使对象不可复制,并在a的接口周围更改以将某些句柄传递给B。
class A {
 public:
  class B {
   public:
    // This should be const, without good reason to make it otherwise.
    B(B&); 
  };
};