Public Group

#### Archived

This topic is now archived and is closed to further replies.

# global variables

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

## Recommended Posts

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 

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
also I still can''t fix this error it keeps on giving me these kind of errors

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

pls some1 help me

##### Share on other sites
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.

cplusplus.com
MSDN

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

##### Share on other sites
thanx for the quick reply

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

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?

##### Share on other sites
quote:
Original post by da_cobra

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

##### Share on other sites
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.

• 10
• 19
• 14
• 19
• 15