Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
13 replies to this topic

#1 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

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

      
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

Sponsor:

#2 Fruny   Moderators   -  Reputation: 1653

Like
Likes
Like

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.

#3 Ibanez   Members   -  Reputation: 122

Like
Likes
Like

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.

#4 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

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++
Help menu, or open the Technical Support help file for more information

#5 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 2001 - 06:08 AM

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

#6 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

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.

Useful links:
cplusplus.com
MSDN

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


#7 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 2001 - 06:23 AM

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?

#8 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 12 December 2001 - 07:16 AM

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!


#9 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 2001 - 07:39 AM

thanx Oluseyi

#10 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

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.

#11 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

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

Which is more information than most people want or need.


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


#12 CaptainJester   Members   -  Reputation: 523

Like
Likes
Like

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.

#13 Askadar   Members   -  Reputation: 122

Like
Likes
Like

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:

#ifndef HEADERFILENAME_DEFINED
#define HEADERFILENAME_DEFINED

//here goes the usual header stuff

#endif

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

#14 BaShildy   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 December 2001 - 03:00 AM

Don''t think anyone else posted about this.
if (bMenu=true) Menu() ; // go to the menu function
if (bGame=true) GameMain() ; // go to the game loop

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

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

or :
if (true == bMenu) Menu() ; // go to the menu function
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.



PARTNERS