开发者

std::vector<std::unique_ptr<UnimplementType>> compile error

Something error with my code. I use forward declaration in my class <RenderPass>, th开发者_运维技巧e std::unique_ptr<RenderPass> and std::list<std::unique_ptr<RenderPass>> work well. But the std::vector<std::unique_ptr<RenderPass>> cause a compile error. Has anyone encountered this situation before? Thank you!

class RenderPass;

class RenderSystem final
{
public:
    RenderSystem() = default;
    ~RenderSystem();

private:
    std::unique_ptr<RenderPass> c {} // work;
    std::vector<std::unique_ptr<RenderPass>> m_render_passes {} // compile error:  error C2338: static_assert failed: 'can't delete an incomplete type';


It's because you do inline initialization of the vector. That requires the full definition of the element type.

If you define the RenderSystem constructor in a source file where you have the full definition of RenderPass you can use its initialization list to initialize the vector.

So the class definition will instead be like:

class RenderPass;

class RenderSystem final
{
public:
    RenderSystem();
    ~RenderSystem();

private:
    // Note: No inline initialization of the vector
    std::vector<std::unique_ptr<RenderPass>> m_render_passes
};

And in a source file:

#include "header_file_for_renderpass.h"

RenderSystem::RenderSystem()
    : m_render_passes {}
{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜