2

宣言を使ってメソッドをモックしたい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() );
    };
}
4

1 に答える 1

4

AとBの定義がないとわかりません。一時的なものからBを構築しようとして失敗しているように聞こえます。これは、一時的なものを非定数参照にバインドできないためです。

たとえば、コピーコンストラクタは次のように定義されます。

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

修正すると、const参照になります。

于 2010-09-13T17:18:22.527 に答える