0

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.

edit: (more details)

FwdClass.cpp implementation includes DerivedHandler.h, so I cannot include FwdClass header DerivedHandler.h as it leads to circular dependency. That is why, I was doing the forward declaration. Forward declaration works fine with raw ptrs, but not with unique ptrs.

I made a DerivedHandler.cpp and included FwdClass.h but that still does not help.

I don't think this is a repeat question, specially not with the circular dependency nuance.

user1745995
  • 141
  • 1
  • 7

0 Answers0