C++ pointer to incomplete class type is not allowed

17,533

You need to add this to Dog.cpp:

#include "AnimalCare.h"

Also, you are missing the ; after your class declaration for Dog, so it should be:

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

std::string GetParentName();
void Feed(void*);
private:
AnimalCare* g_parent;
};
Share:
17,533
Narcis
Author by

Narcis

Updated on June 04, 2022

Comments

  • Narcis
    Narcis almost 2 years

    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?<-

  • Narcis
    Narcis almost 9 years
    Thank You :D, it was so simple. :( That was just an Example code. The actual code is a lot bigger.
  • Mark
    Mark almost 9 years
    No problem. Sometimes just needs a second pair of eyes to take a look. Please accept this answer if it solved your problem. Thanks