Reputation: 53
I'm by no means new to using headers in programs, but I've recently run into a strange problem.
#ifndef PLAYER_HEADER_GUARDS
#define PLAYER_HEADER_GUARDS
#include "CollidableObject.h"
#include "Includes.h"
extern const int ANIMATION_EXTENDER;
extern class MapSystem *mapSystem;
class Player : public CollidableObject
{
//declaration of rest of the class
};
#endif
I have a lot of classes split across several files (and their respective headers), but here is my current problem : even though I have already included CollidableObject.h, the compiler (CodeBlocks) throws an error
Player.h|12|error: expected class-name before '{' token|
The problem arises from the " : public CollidableObject" part of the code, since the program worked fine before I added that. Shouldn't including the CollidableObject header (I'm not posting it, but it basically looks just the same, and defines the class CollidableObject) remove this kind of a problem? Further,
extern class MapSystem *mapSystem;
This statement should, to the best of my knowledge, compile fine without the extra "class" after extern, but if I remove the class keyword, it throws an error "expected type qualifier before * token". The class MapSystem has also been defined previously, and it should work fine - but without the class keyword, it doesn't.
So that brings me to my question - what mistake am I making in my header files, that is leading to such problems? No multi-filed project that I worked on before had such problems.
Upvotes: 2
Views: 122
Reputation: 254431
It sounds like either "CollidableObject.h"
or "Includes.h"
tries to reinclude "Player.h"
, resulting in a circular dependency. The result is that Player
ends up being defined before CollidableObject
, so the latter name is not declared at that point.
Make sure neither of these headers, nor any of their dependencies, include "Player.h"
. You might need to add forward declarations of class Player;
if anything needs to use that name.
Also check that "CollidableObject.h"
does actually define class CollidableObject
(perhaps there's a spelling mistake?), and has a uniquely named include guard.
As for the second question:
extern class MapSystem *mapSystem;
This doubles up as a declaration of class MapSystem
, so doesn't require a previous declaration.
extern MapSystem *mapSystem;
This does not declare MapSystem
(it only indicates that it's a type name, not a class), so it does require a previous declaration.
Upvotes: 5