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
 }
  std::unique_ptr<FwdClass> getFwdClass () noexcept override {
    return std::unique_ptr<FwdClass>(std::move(p));
  }
 std::unique_ptr<FwdClass> p;
}

I see the following compile errors:

**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));

How can I fix these? I have tried to make a DerivedHandler.cpp file with getFwdClass/ default destructor defined and included FwdClass.h but that does not help.

(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 and the interface class Handler.h. Forward declaration works fine with raw ptrs, but not with unique ptrs.

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

user1745995
  • 141
  • 1
  • 7
  • Moving the implementations to .cc file will help. The code snippet can't come, the overridden method returns what? – 273K Sep 24 '20 at 04:49
  • Does forward declaration supports std::move ? – pokche Sep 24 '20 at 05:33
  • How does this differ from your previous [question](https://stackoverflow.com/questions/63986179/forward-declaration-and-unique-ptr)? – Alan Birtles Sep 24 '20 at 05:55

0 Answers0