kooldart
kooldart

Reputation: 61

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.

'Component.h'
#include "Entity.h"
class Component {

public:
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:

'Public.h'

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

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

'Component.cpp'

#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.

Upvotes: 0

Views: 80

Answers (2)

Bhargava Srinarasi
Bhargava Srinarasi

Reputation: 174

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.

Upvotes: 1

kooldart
kooldart

Reputation: 61

Forward declaring the class 'Entity' before the class 'Component' seemed to fix the issue.

Upvotes: 0

Related Questions