Say I have two classes:
"Foo.h"
#pragma once
class Foo
{
public:
Foo()
{
};
~Foo()
{
};
};
"A.h"
#pragma once
#include <memory>
class Foo;
class A
{
public:
A(){};
~A(){};
std::unique_ptr<Foo> foo;
};
A holds a unique_ptr
of Foo
. I didn't want to include Foo
in "A.h", so I forward declared it. By just forward declaring the class Foo
in "A.h", I get a compile time error:
error C2027: use of undefined type 'Foo'
error C2338: can't delete an incomplete type
So I was following this article on how to avoid this error and moved A's destructor in it's own .cpp file where I also include Foo:
"A.cpp"
#include "A.h"
#include "Foo.h"
A::A()
{
}
A::~A()
{
}
After implementing the destructor of A in "A.cpp", I'm able to compile the program, because the class Foo is known in "A.cpp". This seems logical, because unique_ptr needs the complete type to call it's destructor. But to my surprise, after commenting out the constructor of A (in "A.h" as well as "A.cpp"), I get the same error. How is this possible? Why does the compiler complain about beeing not able to call Foo's destructor when A has no constructor?
EDIT: I uploaded the 4 files so you can test the program. I'm using MSVC++ of Visual Studio 2013.
A
does have a constructor when you comment out your constructor: a default constructor is provided by the compiler, and this constructor gets an inline definition. – Mobileunique_ptr
, possibly in case the body of the constructor (or another member) throws an exception and the unique_ptr member needs to be destroyed again. The code you showed above should not have that effect, because there are no other members. – Fossilizeunique_ptr
member, and constructing one of those members can fail with an exception, the unique_ptr member would need to be destroyed. Are you showing the exact code you're testing, or a simplified version that doesn't actually give the same error? – FossilizeBar
is required because the destructor of theunique_ptr
is instantiated. But I do not understand why this instantiation takes place. It seems unnecessary, consideringFoo
has no bases nor other data members. – Mobile