使用前向声明失败创建智能指针

时间:2018-09-03 19:26:12

标签: c++ smart-pointers forward-declaration

所以我有以下内容:

// PE.hpp
#include <memory>
#include <string>

namespace pe { namespace managers {
    class WindowManager;
}}

namespace pe { namespace engine {
    class PE {
    private:
        std::unique_ptr<pe::managers::WindowManager> mp_windowManager;

    public:
        PE(const std::string& windowTitle, windowWidth, windowHeight);
        ~PE() = default;
    };
}}

// PE.cpp
#include "engine/managers/WindowManager.hpp"

namespace pe { namespace engine {
    PE::PE(const std::string& windowTitle, int windowWidth, int windowHeight) :
    mp_windowManager(std::make_unique<pe::managers::WindowManager>(windowTitle, windowWidth, windowHeight)) {
    }
}}

// WindowManager.hpp
namespace pe { namespace managers {
    class WindowManager {
    private:
        int m_windowWidth, m_windowHeight;
        std::string m_windowTitle;

    public:
        WindowManager(const std::string& windowTitle, int windowWidth, int windowHeight);
        ~WindowManager() = default;

        /**
         * Manager setter and getter functions that are inline
         */
}}

// WindowManager.cpp
#include "engine/managers/WindowManager.cpp"

namespace pe { namespace managers {
    WindowManager::WindowManager(const std::string& windowTitle, int windowWidth, int windowHeight) :
    m_windowTitle(windowTitle),
    m_windowWidth(windowWidth),
    m_windowHeight(windowHeight) {
    }
}}

现在,当我使用原始指针时,这绝对可以正常工作。但是当我使用上面的示例时,它会出现以下错误:

/usr/include/c++/7/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = pe::managers::WindowManager]’:
/usr/include/c++/7/bits/unique_ptr.h:268:17:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = pe::managers::WindowManager; _Dp = std::default_delete<pe::managers::WindowManager>]’
/home/jamie-rhys/CLionProjects/PsychoEngine/PsychoEngineCore/includes/engine/PsychoEngineCore.hpp:26:16:   required from here
/usr/include/c++/7/bits/unique_ptr.h:76:22: error: invalid application of ‘sizeof’ to incomplete type ‘pe::managers::WindowManager’
  static_assert(sizeof(_Tp)>0,

我试图寻找可能出问题的地方并试图纠正它,但是对于我一生,我找不到原因。

我已经完成了上述示例告诉我的操作,并确保在WindowManager中有一个dtor(默认情况下并将其放置在.cpp中)。

有什么想法吗?

谢谢

0 个答案:

没有答案