dimanche 20 septembre 2020

forward declaration and unique_ptr [duplicate]

I have the following code.

Handler.h

class FwdClass;

class Handler {
 public:
  virtual std::unique_ptr<FwdClass>
  getFwdClass() noexcept {
    return nullptr;
  }
  virtual ~Handler() {
  }
};

DerivedHandler.h

#include "Handler.h"

class DerivedHandler: public Handler {
 explicit DerivedHandler() { // error here
 }
  getFwdClass () noexcept override {
    return std::unique_ptr<FwdClass>(std::move(p));
  }
 std::unique_ptr<FwdClass> p;
}
**error:** invalid application of 'sizeof' to an incomplete type 'FwdClass'
        static_assert(sizeof(_Tp)>0,

and also:

note: in instantiation of member function 'std::default_delete<FwdClass>::operator()' requested here
          get_deleter()(std::move(__ptr));

Why am I seeing these errors? Best way to fix this kind of a thing would be much appreciated.

I cannot include FwdClass.h because FwdClass.cpp needs to include DerivedHandler.h and to avoid circular dependency, I created interface class Handler.h and did forward decl. It was working well with raw pointer, but barfs with unique_ptr.

Aucun commentaire:

Enregistrer un commentaire