Lets say there are two classes: Map and Entity. Map has an array of pointers to all Entities in the game, and the entity uses a map to check if there is an empty spot where he is going to go. So, in the main function, I am doing the following:
map->getEntity(x, y)->walk(map)
which causes a circular dependency between the entity and the map. I tried moving the walking logic of an entity to the Map class, but it caused many other problems, and it was not quite right in my opinion, because i wanted to encapsulate all the logic inside an entity.walk() method (checking whether target field is reachable by the the entity, whether is empty, etc), so i dont need to check all of this in my main function before i move an entity. I also tried using forward declaration, but it can't help in this situation, because both map and entity are using each other's methods, which are not declared yet. Here's what I have now:
class Entity;
class Map {
public:
void add(Entity* ent) {
ent->getX() //Map.h
//...
}
}
#include "Map.h"
class Entity {
void walk(Map* map) {
if(map->isEmpty(x, y)) {
//... //Entity.h
}
}
}