سؤال

أريد أن أسخر من طريقة مع الإعلان 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 من A مؤقتًا وفشلًا لأنه لا يمكن أن يربط المؤقتة إلى مرجع غير مؤكد.

على سبيل المثال ، قد يتم تعريف منشئ النسخ الخاص بك على النحو التالي:

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

مع الإصلاح فقط جعله مرجعا const.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top