I have a problem and I can't understand any of the solutions online. It's a file management problem.
I have 5 files:
- Sprite.cpp, Sprite.h
- Game.cpp, Game.h
- Main.cpp
Sprite and Game cpps #include their header counterpart.
I have class Sprite defined in Sprite.cpp
I want Game to store all the global instances in my program, such as arrays of Players, Enemies and Sprites which are accessable to every file in my program.
The problem is Sprite becomes dependant on Game to access these global variables. But Game
is dependant on Sprite to create the array of Sprites. This causes problems when they try to import each others header files.
The exact code is this (each header has the #ifndef NAME_INCLUDED thing):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
//sprite.h:
#include "game.h"
class Sprite
{
public:
Sprite();
};
//sprite.cpp:
#include "sprite.h"
Sprite::Sprite(){}
//game.h:
#include "sprite.h"
extern Sprite sprite;
//game.cpp:
#include "game.h"
Sprite sprite;
//main.cpp:
#include "game.h"
int main()
{
return 0;
}
|
The error message I get:
Error 1 error C2146: syntax error : missing ';' before identifier 'sprite'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Is their a solution to this problem or can you suggest alternative ways of organising data that needs to be accessed by many classes?
Thanks