Sam Martens Sam Martens - 10 months ago 32
C++ Question

Why I can't use std::unique_ptr for avoiding circular dependency?

Here is my piece of code:

class Model;

class Resources
Resources() :
, pathToSkyBoxModel("E:\\C++\\OpenGLtutorial\\resources\\cube.obj")

void Init(const ShaderProgram& shaderProgram);

/* Setters */
void SetSkyBoxModelPath(std::string&& newPath) { pathToSkyBoxModel = newPath; };

/* Getters */
bool IsInitialized() const noexcept { return initialized; };
const std::string& GetPathToSkyBoxModel() const noexcept { return pathToSkyBoxModel; };

DiffuseTexture default_texture;
TransparentTexture default_transparent_texture;


std::unique_ptr<Model> pModel;
bool initialized;

I am trying to avoid circular dependency by using std::unique_ptr for Resource class member pModel. Unfortunately, I get compilation error like: "You can't use here partly defined class". But it works for std::shared_ptr and common pointer. What's wrong with std::unique_ptr ?


Problem is that the compiler tries to declare an inline destructor, and for that it needs the full definition of the class.

You can get around it by declaring a destructor in the .h and defining it in the /.cpp

//in .h

//in cpp
Resources::~Resources() {}