Can't use std::unique_ptr with T being a forward declaration

前端 未结 2 955
梦毁少年i
梦毁少年i 2021-01-07 18:46

Now first, I am aware of the general issues with unique_ptr<> and forward declarations as in Forward declaration with unique_ptr? .

Consider these three files:

2条回答
  •  温柔的废话
    2021-01-07 19:39

    You also need to put A's constructor in C.cpp:

    A.h

    #include 
    #include 
    
    class B;
    
    class A {
    public:
         A();
        ~A();
    
    private:
        std::unique_ptr m_tilesets;
    };
    

    C.cpp

    #include "A.h"
    
    class B {
    
    };
    
    A::~A() {
    
    }
    
    A::A() {
    
    }
    

    See this answer. The constructor needs access to the complete type as well. This is so that it can call the deleter if an exception is thrown during construction.

提交回复
热议问题