lundi 20 juillet 2015

C++ pointer to incomplete class type is not allowed

I'm trying to create an entity system. Each entity has a list of components and each component has a pointer to the parent Entity.

========================Example Code:=========================

---------------Dog.h--------------------
    #include <string>

    class AnimalCare;
    class Dog
    {
    public:
    Dog(AnimalCare* parent);

    std::string GetParentName();
    void Feed(void*);
    private:
    AnimalCare* g_parent;
    }
    ----------------------------------------

    ----------------Dog.cpp-----------------
    #include "Dog.h"

    Dog::Dog(AnimalCare* parent){
     g_parent =parent;
    }

    void Dog::Feed(void* food){
     //TODO: Feeding
    }

    std::string Dog::GetParentName(){
    [1]return parent->GetName();
    }
    ----------------------------------------

    ---------------AnimalCare.h-------------
    #include "Dog.h"

    class AnimalCare{
    public:
       AnimalCare(std::string name);

       std::string GetName();
       void InitDog();
    private:
       std:string g_name;
       Dog* g_dog;
    };
    ----------------------------------------

    --------------AnimalCare.cpp------------
    #include "AnimalCare.h"

    AnimalCare::AnimalCare(std::string name){
       g_name =name;}

    std:string AnimalCare::GetName(){
      return g_name;}

    void AnimalCare::InitDog(){
      g_dog = new Dog(this);}

============================================================

[1] -> I'm getting pointer to incomplete class type is not allowed. I know, it is because AnimalCare class in Dog header is just declared but not defined. ->Is there a way to get around this?<-

Aucun commentaire:

Enregistrer un commentaire