kooldart kooldart - 11 months ago 38
C++ Question

pointer variable of class 'Entity' within class 'Component', gives errors even though 'Entity.h' has been included

Okay So I'm making my own Entity Component System, and I'm trying to have an Entity pointer object within a Component class, but the object(variable) gives me a ton of errors, even though it has been included.

#include "Entity.h"
class Component {

Entity* ThisEntity;


That doesn't work, and gives me 76 errors inside 'Entity.h', all of which don't recognize my custom types(like Component, string, etc).
I usually use a global header file which has all the global variables, and includes everything necessary, like this:


#ifndef Entity_h
#include "Entity.h" // Entity is include guarded

When i try to include 'Public.h' inside 'Component.cpp' it still gives me errors:


#include "Public.h"
#include "Component.h" // The arrangement is correct, public before component but this still doesn't work

When I hover over the pointer variable "Entity* ThisEnt" it shows me "Entity Class", so it recognizes it but it still gives me 76 errors.

Answer Source

You can not refer to header files from each other as it creates a cyclical dependency. You can break that dependency by forward declaring one of the classes you want to use.