Can’t delete an incomplete type with std unique_ptr of virtual class


This Content is from Stack Overflow. Question asked by SADL

I have a multi inheritance here an example:

A virtual class Foo:

class Foo{
std::string m_name;
Foo(){std::cout << "constructor foo" << std::endl;}
Foo(std::string name); // another constructor defined in Foo.cpp
virtual ~Foo(){std::cout << "destructor foo" << std::endl;}
virtual std::string power(){return NULL;}

Foo::Foo(std::string const& name) :
    m_name(name) {
    std::cout << "m_name =  " << m_name << std::endl;

I have another class Roo that inherits from Foo:

class Roo: public Foo{
void create() override;
Roo(std::string const&);
virtual ~Roo(){std::cout << "destructor Roo" << std::endl;}
bool configure(json const&) override;

Roo::Roo(std::string const& name) :
Foo(name) {

a class AA that inherits from Roo:

class AA: public Roo{

AA(std::string name);
void create(){...}

I get a compile error “can’t delete an incomplete type” in my class main.cpp at this level :

std::unique_ptr<Foo> rp = std::unique_ptr<Foo>(std::make_unique<Foo>(AA(name)))

the error appear in memory:

void operator()(_Ty* _Ptr) const noexcept /* strengthened */ { // delete a pointer
static_assert(0 < sizeof(_Ty), "can't delete an incomplete type");
delete _Ptr;      

Why Foo is incomplete type ?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?