# Compiling Error With Game States

This topic is 2600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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).

##### Share on other sites

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).

##### Share on other sites
Thanks a bunch, I read the article and understood what I was doing wrong.

• 10
• 48
• 12
• 10
• 10
• ### Forum Statistics

• Total Topics
631383
• Total Posts
2999680
×