Jump to content
  • Advertisement
Sign in to follow this  

Compiling Error With Game States

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

If you intended to correct an error in the post then please contact us.

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

Thanks in advance.

Share this post

Link to post
Share on other sites

It sounds like you should read this article.


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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!