user10873462
user10873462

Reputation: 21

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';

Upvotes: 1

Views: 202

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409412

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 {}
{
}

Upvotes: 7

Related Questions