Compiling Error With Game States

Started by
2 comments, last by JonathanO'Brien 12 years, 11 months ago
So I am attempting to compile a program for a simple game I am making for an assignment, however I have encountered an issue.


GameState *currentState = NULL;

Is a variable that is declared twice - in two source files; main.cpp and gamestates.cpp.

When it is declared twice, the compiler returns the following:
1>main.obj : error LNK2005: "class GameState * currentState" (?currentState@@3PAVGameState@@A) already defined in gamestates.obj

Then, if declared only in main.cpp, the compiler returns:

1>c:\users\9101757\documents\visual studio 2010\projects\motion\motion\gamestates.cpp(61): error C2065: 'currentState' : undeclared identifier

I get a series of errors of the same style (only related to gamestates.cpp - the rest compiles successfully).

Then, if only declared in gamestates.cpp - not main.cpp, I get:

1>c:\users\9101757\documents\visual studio 2010\projects\motion\motion\main.cpp(485): error C2065: 'currentState' : undeclared identifier

and another series of errors (only from main.cpp).


I was hoping that someone could tell me how to solve this error without porting code between files. I can successfully carry each function/variable from gamestates.cpp over to main.cpp and compile successfully (as GameState *currentState = NULL; is only declared once).

Thanks in advance.
-A1P4A 0M3GA
Lead script writer on Scutum [http://www.gamedev.n...-entertainment/]
Team Member of [size=2]Forcas Entertainment
Amateur programmer with C++ and SDL knowledge
Game Enthusiast
Advertisement
It sounds like you should read this article.

It sounds like you should read this article.


Right.

The summary, once you have read all of that and fully understood it: put it in gamestates.cpp (because that will actually have a corresponding header file), and declare it as 'extern' in gamestates.h. #include the header from whereever it's needed (i.e. from main.cpp, if you don't already).
Thanks a bunch, I read the article and understood what I was doing wrong.
-A1P4A 0M3GA
Lead script writer on Scutum [http://www.gamedev.n...-entertainment/]
Team Member of [size=2]Forcas Entertainment
Amateur programmer with C++ and SDL knowledge
Game Enthusiast

This topic is closed to new replies.

Advertisement