2

Here is my piece of code:

class Model;

class Resources
{
public:
  Resources() :
      initialized(false)
    , 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;

  private:

  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 ?

  • Another duplicate: [Forward declaration with unique_ptr?](http://stackoverflow.com/questions/13414652/forward-declaration-with-unique-ptr) – iammilind Aug 16 '16 at 05:00

1 Answers1

4

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
~Resources();

//in cpp
Resources::~Resources() {} 
dau_sama
  • 4,247
  • 2
  • 23
  • 30
  • But why it works with std::shared_ptr ? – Sam Martens Aug 16 '16 at 05:10
  • 1
    http://stackoverflow.com/questions/20985667/why-doesnt-stdshared-ptr-need-to-know-complete-type-if-its-constructed-from shared_ptr has an extra indirection, so you need to know the full type only when you actually construct/delete an object. – dau_sama Aug 16 '16 at 05:14