• 9
• 10
• 10
• 11
• 16

# include and global variables problems

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

## Recommended Posts

HI , it seems that i allways got some problems that need attention yesterday VS 2003 include libraries and now #include. Can someone take a look at my project at http://shareitagain.com/files/1053534628/1658163095_Zarcanoid.html Problem is that i have game.cpp where is my winmain() and in it i include game.h where i include all the other includes and define the globals.Problem is that in game.h I include gamelevels.h and in gamelevels.h i include level1.h where i have loadlevel1() function which i use in loadlevel() function (in winmain) which is defined in loadlevel.h and included by game.h.Problem is that i cannot use my global variable DX in loadlevel1 function. I know tis is somewhat unreadable but if you can please download my source and look for loadlevel functions (i posted whole project - 12kb ) thanks, zlatko

##### Share on other sites
int GAMESTATE = LOADMENU;int LEVEL = 1;HWND g_hWnd = NULL;static std::vector<Z_Sprite *> SpritesManager;float  g_fElpasedTime;double g_dCurTime;double g_dLastTime;ZwinClass*     win = NULL;ZD3DGraphics*  dx = NULL;

Let's briefly analyse the above piece of code, which can be found in your game.h.
All declarations that are not marked as static will be put into each and every compilation unit (cpp-file) that includes them, thus leading to linker errors due to duplicate names.
Quick solution: define them in one cpp file and declare them extern
// globals.cppint GAMESTATE = LOADMENU;int LEVEL = 1;HWND g_hWnd = NULL;float  g_fElpasedTime;double g_dCurTime;double g_dLastTime;ZwinClass*     win = NULL;ZD3DGraphics*  dx = NULL;

// game.hextern int GAMESTATE;extern int LEVEL;extern HWND g_hWnd;extern float  g_fElpasedTime;extern double g_dCurTime;extern double g_dLastTime;extern ZwinClass*     win;extern ZD3DGraphics*  dx;

What about the vector? The vector is declared "static" (which is deprecated by the way) and as such will be locally defined in each compilation unit that includes game.h. As a consequence, each cpp file will have its own copy of
"SpritesManager" and that will lead to the funniest errors imaginable.

You have a few possibilities to get a quick solution:
1) Do the same as with the other globals and define it in one cpp file and
delcare it "extern" in the header.
2) Encapsulate it - together with all the other globals - in a singleton or
monostate class.
3) Access the sprites vector via a function, and have the vector itself in a
single cpp file using an anonymous namespace:
// game.htypedef std::vector<Z_Sprite *> SpriteVector;SpriteVector & GetSprites();

// somefile.cppnamespace {   SpriteVector sprites;}SpriteVector & GetSprites() {   return sprites;}

HTH,
Pat

##### Share on other sites
Posting the source code directly in here would help a lot:

void loadLevel(int&GAMESTATE,int& LEVEL){switch (LEVEL){case 1:	{		loadLevel1();		LEVEL = 1;	}}GAMESTATE = INLEVEL;};void loadLevel1(){	//Z_Sprite * back = new Z_Sprite(dx->pD3DDevice,"back.bmp",800,600);	//SpritesManager.push_back();};

It seems that the problem is that you're unnecessarily making too many .h then you need. You might want to read through Kylotan's Article.

##### Share on other sites
Thx guys for that static Z_sprite i left it that way when i was trying to make it global.Now i must go somewhere and when i come back i ll tell you what i have done.I'm sure that external would help.

Can you post me a some online references for c++ about
external , __cdccl ,static,singleton,monoton what they means cause i need to learn about them. :)