OK, I'm in a dilemma right now, and neither my knowledge of C (which isn't the biggest one anyways) nor the almighty Google seem to be able to help me with this:
I have some structures for a game prototype:
Map
(Uhhm... the Map..)Chara
(A base for Enemies the Players)Player
(The Player)
Now the problem is: Map
needs a reference to the Player
that's on it, Player
is being constructed giving it a Map
and a Chara
, and Chara
needs the Map
too.
If I declare the structs in the header files and wrap them with #ifndef
I run into a cyclic dependency loop when I include headers from within other headers.
When I declare the structs in the .c files, I use extern struct Map map
etc. but then I run into problem like invalid use of incomplete declaration
or forward declaration of struct XXX
.
It's 4 o'clock in the morning here and I would like to spend my time more on rewriting the engine stuff (which already exists both in Python AND JavaScript...yes I've got too much time!) rather then trying every feasible combination of search terms for the rest of the night.
I'm aware that this might be a REALLY easy thing, but it has 30°C in here, so please have mercy with my C "skills" ^^
EDIT
Since my problem used typedefs and caf's answer didn't include them, I had to fiddle a bit more with it in order to get it all working. So to help the people which might find this answer via a SE I'll add the code below:
map.h
typedef struct _Player Player;
typedef struct _Map {
...
Player *player;
...
} Map;
map.c
// include both player.h and chara.h
player.h
typedef struct _Map Map;
typedef struct _Chara Chara;
typedef struct _Player {
Chara *chara;
...
} Player;
Player *player_create(Map *map, int x, int y);
player.c
// include player.h, chara.h and map.h