简体   繁体   中英

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

Something error with my code. I use forward declaration in my class <RenderPass>, the 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 {}
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM