"Use undefined type" with uniqueptr to forward the declared class and default constructor / destination

In the following code, is the only way to avoid a compilation error and enable the Bh implementation of the constructor / move destination manually in A.cpp?

// A.h
#include <memory>
class B; // implementation somewhere in B.h/B.cpp
class A
{
public:
    A() = default;
    ~A() = default;
    A(const A&) = delete;
    A& operator=(const A&) = delete;
    A(A&&) = default;
    A& operator=(A&&) = default;

private:
    std::unique_ptr<B> m_b;
};

Visual Studio 2015 gives "error C2027: use of type undefined" because the constructor / destination operator std :: unique_ptr calls deleter on m_b (tries to call the B destructor), which is obviously unknown at this point.

+4
source share
1 answer

Yes, you need to have access to the full definition B, wherever you enter std::unique_ptr<B>::~unique_ptr, because it needs to call the Bdestructor.

, A::~A A.cpp, B.h.

+6

Source: https://habr.com/ru/post/1659620/


All Articles