Advertisement

global variables

Started by December 11, 2001 02:13 PM
12 comments, last by da_cobra 22 years, 11 months ago
there is a thing that I still don't quite understand and so I keep on getting these kind of errors : say I have a boolean in my windows message loop to know where I am : in the menu, in the game, like this :
      
while (true)
		{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))			
			{
			if (msg.message==WM_QUIT) 
				// Have We Received A Quit Message?

				{
				// If So break

				break; 
				}
			else
				{
				TranslateMessage(&msg) ;	
				DispatchMessage(&msg) ;		
				} // end of else

			} // end of if

		else
			{
			MyGame.CLS() ; // clear the screen

			if (bMenu=true) Menu() ; // go to the menu function

			if (bGame=true) GameMain() ; // go to the game loop

			}
		} // end of while

	return 0 ;
	} // end of WinMain

      
BUT I have to use these booleans in my menu() function so menu option one makes bMenu to true, Menu option 2 => bGame=true so now I put these booleans into my "Globals.h" and include this file in my Winmain.cpp and in my menu.cpp and I also use the "globals.h" in another file clock.cpp but not for these booleans but for other global variables and/or pointers.... so now I get the following error => inMain.obj : error LNK2005: "bool bExit" (?bExit@@3_NA) already defined in Clock.obj WinMain.obj : error LNK2005: "bool bCredits" (?bCredits@@3_NA) already defined in Clock.obj WinMain.obj : error LNK2005: "bool bGame" (?bGame@@3_NA) already defined in Clock.obj WinMain.obj : error LNK2005: "bool bMenu" (?bMenu@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bExit" (?bExit@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bCredits" (?bCredits@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bGame" (?bGame@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bMenu" (?bMenu@@3_NA) already defined in Clock.obj I know that this error is because I define these booleans over and over, right? How can I avoid this? thanx in advance... Edited by - da_cobra on December 11, 2001 3:14:39 PM Edited by - da_cobra on December 11, 2001 3:15:44 PM
In the header file, you have to declare nonlocal variables as ''extern''.

extern bool bExit; ... and so on

And there is an error in your code : if (bMenu=true) Menu() should be ==. Same on the next line.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
A better way to do it is instead of using 2 booleans to determine program state, use a single int (or better - an enum), that uses, say 0 for menu and 1 for game, 2 for highscore table, 3 for title screen, etc. It makes it easier to put more states in the game, and eliminates a potential problem that your code has: that it is possible to be in both a game state AND a menu state.
btw how come when I try to use an enumerated type I get the following error?

enum MenuChoice { NewGame, Credits, Exit} ;


fatal error C1001: INTERNAL COMPILER ERROR
(compiler file ''msc1.cpp'', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
also I still can''t fix this error it keeps on giving me these kind of errors

WinMain.obj : error LNK2005: "bool bMenuFirstRun" (?bMenuFirstRun@@3_NA) already defined in menu.obj
WinMain.obj : error LNK2005: "bool MenuKeyDown" (?MenuKeyDown@@3_NA) already defined in menu.obj
WinMain.obj : error LNK2005: "bool MenuKeyUp" (?MenuKeyUp@@3_NA) already defined in menu.obj
WinMain.obj : error LNK2005: "int iMenu" (?iMenu@@3HA) already defined in menu.obj
menu.obj : error LNK2001: unresolved external symbol "int MenuChoice" (?MenuChoice@@3HA)
WinMain.obj : error LNK2001: unresolved external symbol "int MenuChoice" (?MenuChoice@@3HA)
Debug/class project.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

pls some1 help me
quote: Original post by da_cobra
btw how come when I try to use an enumerated type I get the following error?

The error lies elsewhere. Keep looking.

quote: also I still can''t fix this error it keeps on giving me these kind of errors

a.) Did you declare the boolean extern in the other header/source file?

b.) Did you actually declare that integer?

An extern declaration tells the current compilation unit (such as a file) that another unit will actually declare and define that variable. One (and only one) of your source files must then declare the variable without the extern keyword.

Useful links:
cplusplus.com
MSDN

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Advertisement
thanx for the quick reply

1) no the error isn''t somewhere else, when I delete that line

enum MenuChoice {...........

the error is gone

2) if I use extern, then I have to declare that variable in the H-file and in the source-file?!?

I don''t understand this, isn''t there any tutorials or such on the web about this topic?
quote: Original post by da_cobra
enum MenuChoice {...........

C++ Enumeration Declarations
User-defined data types: typedef, union and extern.

quote: I don''t understand this, isn''t there any tutorials or such on the web about this topic?

The extern Storage-Class Specifier

Read the documentation, young Jedi.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
thanx Oluseyi
it works like this: every variable and function must be defined in exactly one cpp file. They must also be declared in every single cpp file that uses them. When dealing with functions what you do is you put the function definition in one cpp file and the declaration (the signature) in all the other cpp files that use it. Using .h files to put the declarations where you need them is just a convenience. Header files aren''t compiled, they just get copied into every cpp file that includes them.

So you have to do the same thing with variables too. However when you declare a variable use have to use extern. When you define a variable you leave off the extern. So if you have a variable in multiple files all but one of the files has to have an extern in front of the variable. The easy way to do this is to include the variable in a header and put the extern there. Oh and here''s something I didn''t find out for quite a while: static member variables (which have no relation to static global variables that I know of) need to be initialized in their definitions, and you don''t put static on the definition.

This topic is closed to new replies.

Advertisement