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.