View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# global variables

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1da_cobra  Members

Posted 11 December 2001 - 08:13 AM

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 

### #2Fruny  Moderators

Posted 11 December 2001 - 08:31 AM

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.

### #3Ibanez  Members

Posted 11 December 2001 - 10:44 AM

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.

### #4da_cobra  Members

Posted 12 December 2001 - 05:27 AM

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++

### #5da_cobra  Members

Posted 12 December 2001 - 06:08 AM

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

Debug/class project.exe : fatal error LNK1120: 1 unresolved externals

pls some1 help me

### #6Oluseyi  Members

Posted 12 December 2001 - 06:14 AM

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!

### #7da_cobra  Members

Posted 12 December 2001 - 06:23 AM

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?!?

### #8Oluseyi  Members

Posted 12 December 2001 - 07:16 AM

quote:
Original post by da_cobra

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

quote:

The extern Storage-Class Specifier

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

### #9da_cobra  Members

Posted 12 December 2001 - 07:39 AM

thanx Oluseyi

### #10Anonymous Poster_Anonymous Poster_*  Guests

Posted 12 December 2001 - 10:19 AM

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.

### #11Oluseyi  Members

Posted 12 December 2001 - 12:58 PM

quote:
Header files ...get copied into every cpp file that includes them.

Not exactly or necessarily. It''s pretty close though. A header file should be parsed only once; at compile time all declarations become global, allowing for resolution of all references. This is why we use inclusion guards (#ifndef MY_HEADER_H...) or platform-specific instructions (#pragma once).

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

### #12CaptainJester  Members

Posted 14 December 2001 - 02:21 AM

If you use those inclusion guards, you do not have to declare the variables as extern.

---
Make it work.
Make it right.
Make it fast.

Posted 14 December 2001 - 02:53 AM

If you define a variable in a cpp file, use extern in other files to tell the compiler that it exists.

If you define a variable in a header file, make it include safe using this pattern:

//here goes the usual header stuff

#endif

This way it will be included only once when compiling the whole project.

### #14BaShildy  Members

Posted 14 December 2001 - 03:00 AM

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

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

or to prevent these problems from compiling:
if (bGame) GameMain() ; // go to the game loop

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

if you don''t understand this, you need to read more about how operators work in C/C++. There a lot more complex then what they look at times.

Code comment of the week:
bool hack = false; // DO NOT CHANGE THIS EVER

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.