First let's reproduce the problem in the smallest amount of code:
#include <memory>
class SomeClass;
int main()
{
std::unique_ptr<SomeClass> ptr;
}
error:
In file included from /opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/memory:81:0,
from <source>:1:
/opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = SomeClass]':
/opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/bits/unique_ptr.h:236:17: required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = SomeClass; _Dp = std::default_delete<SomeClass>]'
<source>:7:30: required from here
/opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/bits/unique_ptr.h:74:22: error: invalid application of 'sizeof' to incomplete type 'SomeClass'
static_assert(sizeof(_Tp)>0,
^
Compiler exited with result code 1
Same problem here (to prove that it's nothing to do with inheritance):
#include <memory>
class SomeClass;
class NotDerived
{
// ~NotDerived(); //defined in cpp
std::unique_ptr<SomeClass> ptr;
};
int main(){
NotDerived d;
}
error:
In file included from /opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/memory:81:0,
from <source>:1:
/opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = SomeClass]':
/opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/bits/unique_ptr.h:236:17: required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = SomeClass; _Dp = std::default_delete<SomeClass>]'
<source>:5:7: required from here
/opt/gcc-explorer/gcc-6.2.0/include/c++/6.2.0/bits/unique_ptr.h:74:22: error: invalid application of 'sizeof' to incomplete type 'SomeClass'
static_assert(sizeof(_Tp)>0,
^
Compiler exited with result code 1
Now let's remember what unique_ptr really is:
template<
class T,
class Deleter = std::default_delete<T>
> class unique_ptr;
And default_delete ...
Calls delete
on ptr
And delete
on the pointer (to SomeClass
) will want to destruct the Someclass
, so it will need to call SomeClass::~SomeClass
Which you have not yet declared. Hence the error.
Why is this relevant?
Because in your code, if you don't declare a destructor for Derived
, a default one is generated, which of course will call the destructor of ptr
.
At this point, the compiler will need a complete definition of SomeClass
so that it knows how to destroy it.
By declaring the destructor in Derived, you are deferring this problem to the imlementation of Derived::~Derived
.