Home > Software engineering >  std::vector<std::unique_ptr<UnimplementType>> compile error
std::vector<std::unique_ptr<UnimplementType>> compile error

Time:12-07

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

CodePudding user response:

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