Strange header problems...

Started by
11 comments, last by ahayweh 16 years, 9 months ago
I just ran into a baffling problem with a header file(s). I need some of the types , definitions in a file, say player.h. For all other files that include this, there is no problem. But once I try including player.h in say sprite.h, some of the structs are being undefined. I always use the #ifndef/#endif... Any ideas? Does thing have anything to do with the order the files are #include'ed in? If so, then why?
Advertisement
What exactly is the error? And you say u do ifndef/endif.. thats at the beginning and end of the header file?

Also what are the names of the structs? make sure they are not generic(as you prolly know already).

Also are you going to be using them as objects or just pointers?

EDIT: Also note that make sure yer define is not used twice(in different files).

--Brad
--X
Compiling...
kamikaze.cpp
e:\program files\microsoft visual studio\myprojects\retro_type\game.h(33) : error C2061: syntax error : identifier 'pPlayer_t'
e:\program files\microsoft visual studio\myprojects\retro_type\game.h(34) : error C2061: syntax error : identifier 'pPlayer_t'
main.cpp
e:\program files\microsoft visual studio\myprojects\retro_type\game.h(33) : error C2061: syntax error : identifier 'pPlayer_t'
e:\program files\microsoft visual studio\myprojects\retro_type\game.h(34) : error C2061: syntax error : identifier 'pPlayer_t'
e:\program files\microsoft visual studio\myprojects\retro_type\main.cpp(77) : error C2660: 'G_ProcessCannon' : function does not take 2 parameters

pPlayer_t is of type Player_t*, which is properly defined, and used elsewhere in the program. And the function is properly declared and defined ( and utilized ), with no problem until I try adding game.h to another header file... Yeah, no generic types either.
Did you copy and paste your include guards? If you did you might have given two headers files the same defines which causes one not to be included properly.
Nope, I put them all in manually ( __GAME_H__ and _PLAYER_H__ , __<filedesc>_H__ for each unique file ). I am researching the C2061 error, but still haven't come up w/anything...
It sounds like you're using the headers in a way that causes circular references...

Using forward-declarations should solve the problems, i.e:

//PLAYER.H:#include "sprite.h"class sprite;class player{//...your code};//SPRITE.H#include "player.h"class sprite{//...your code};


Just a thought, may be or may not be the problem thou. :)
"Game Maker For Life, probably never professional thou." =)
Do you have any cyclical dependencies in your headers?

EDIT: As Rasmadrak noted. Although having your sprite class depend on your player class is an punishable offense in at least seven countries.
Thanks for the forward dec tip. I am working on trying that... Agreed, about the backward sprite-player dependency. Here are the offending files :

I am trying to add the powerups, from the game.h file to the player struct. Note that the game.h include and last function prototype are commented out due to causing errors.



[Edited by - ahayweh on July 14, 2007 3:39:55 PM]
Well, including the game.h header would first include (before defining anything) the player.h header, which would include the game.h header again—but the include guards would object to the second inclusion, yielding an empty file. Then, the compiler would encounter a function definition with a powerup argument, and notice that no such type has been defined yet.

The solution would be to move powerups to their own file, in order to break the circular dependency.
Darn it. I thought it was working, but then I deleted the comments off the proptype, and the compiler isn't seeing the types in the other header.... Well, at least I can include the header w/o any errors and thats progress...

This topic is closed to new replies.

Advertisement