-1

I have a file game.h which has this declaration

typedef struct Enemy {
  ...
}Enemy;

And a function

void moveEnemy(Level* l, Enemy* enemy){
  ...
}

Level is declared on levels.h, so in game.h I have:

#include "levels.h"

Everything was perfect, until I had to use Enemy in levels.h.

So, in levels.h I added:

#include "game.h"

And now I get a compilation error:

game.h:34:2: error: unknown type name ‘Level’
  Level* level;
  ^

I have include guards on both .h

I don't know why I can't have on file including another.

What can I do?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aleksandrus
  • 1,589
  • 2
  • 19
  • 31
  • I don't understand the question, is `moveEnemy()` defined in the header file? If it is, it should not! – Iharob Al Asimi Jan 12 '16 at 14:25
  • Yes, it is. I'll put it in a .c to see if it works – Aleksandrus Jan 12 '16 at 14:26
  • 1
    No, see the answer. But YES never put functions in header files. Header files are meant ot include function prototypes, constants and declarations, definitions of structures or user defined data types, etc. Not function definitions, those belong to *.c* files. – Iharob Al Asimi Jan 12 '16 at 14:27
  • Use 'extern'. http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c – vpenkoff Jan 12 '16 at 14:28
  • Have you tried this http://stackoverflow.com/questions/3246803/why-use-ifndef-class-h-and-define-class-h-in-h-file-but-not-in-cpp ? – ganchito55 Jan 12 '16 at 14:39
  • Tried what? Include guards? – Aleksandrus Jan 12 '16 at 14:59

1 Answers1

1

Just add a forward declaration, like this in game.h before the function,

typedef struct Level Level;

since it's just a pointer to Level this will do it.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97