I’m modifying the game state tut from http://lazyfoo.net. Got it to work as single .cpp; I’ve separated all my code into multiple files and am now running into an issue. I’m trying to initialize in globals.h file and then declare in globals.cpp file. Other .cpp files need access to the currentState pointer without multiple inclusion issues.
//globals.h
cGameState *currentState;
//globals.cpp
//Game state object
cGameState *currentState = NULL;
I’ve tried adding Extern before the initialization in header file and it throws an error not liking the type. Is there such a thing as a global pointer? Sorry if I’ve used the wrong vocabulary I’m far from an expert, yet I feel so close; I’m still missing something.
externis exactly what you want in the header file, but it needs to be lowercase E. Your terminology isn’t quite right – your header file needs a declaration whilst exactly one (thanks to the “One definition rule”) .cpp file wants a definition.E.g. in the header file:
and in the .cpp file:
Personally though I’d look to avoid having lots of global state in your application.