Page 1 of 1

Where do I declare global variables?

Posted: Sun Jul 01, 2007 11:47 am
by Weng
I have a C++ project with multiple .h and .cpp files and with a .cpp file containing the int main() function.

Where do I declare global variables such that they can be accessed by the methods in the various .cpp files?

I declared the variable before the main() function but there is an "undeclared variable" error.

I tried creating a .h file to store the global variable and added the line: #include "globals.h" in the necessary .h files but there were all sorts of link errors.

:?

Posted: Sun Jul 01, 2007 11:59 am
by eneru
i believe you should declare each global variable you are using in each cpp as being extern (ex : "extern int i" for a global "int i" variable)

Posted: Sun Jul 01, 2007 5:27 pm
by vitek
You should have no problems declaring global variables in a common header. Just make sure to add the extern linkage specifier to each declaration.

I feel it is my duty to tell you that global variables are evil and you should avoid them if at all possible.

Travis

Posted: Mon Jul 02, 2007 2:05 pm
by Weng
I followed the steps but there are still linker errors.

First I declared the variable in the Global.h file:
extern bool laser;

Then I initialised the laser global variable in the .cpp file which contains the main() function:
laser = false;

There are 2 more .cpp files which use the laser variable and they are used like this:

if(laser)
..........

Lastly, I added the line include "Global.h" in the necessary .h files.

What's wrong? :?
And also , where should I initialise the global variable?

Posted: Mon Jul 02, 2007 4:16 pm
by vitek
It sounds like you just aren't defining the global variable. In any one of your .cpp files, add

Code: Select all

bool laser = false; // or true
The extern keyword tells the compiler/linker that the variable is defined elsewhere. You need to keep with that promise and actually define the variable.

Travis

Posted: Mon Jul 02, 2007 4:28 pm
by CuteAlien
Weng wrote:I followed the steps but there are still linker errors.

First I declared the variable in the Global.h file:
extern bool laser;
OK
Weng wrote: Then I initialised the laser global variable in the .cpp file which contains the main() function:
laser = false;
use:
bool laser = false;
Weng wrote: There are 2 more .cpp files which use the laser variable and they are used like this:

if(laser)
..........

Lastly, I added the line include "Global.h" in the necessary .h files.
I hope you included it in the necessary .cpp files.
Weng wrote: What's wrong? :?
And also , where should I initialise the global variable?
Initialize it outside of all scopes.
For example in main.cpp after the includes but before any functions.

But like vitek already mentioned having a global variable is mostly evil and for a variable called laser I'm nearly certain that it should belong into a class. It's fine now for finding out how global variables do work, but once you have that start the following way:

Create a main class for your application. I call it usually 'class App'. Now create one global variable for that class for example like this:
In the header you have:

Code: Select all

extern App APP;
In the source you have:

Code: Select all

App APP;
Now for the start you can put all variables in this class. And from all other files you can always access them via APP.variable;
When you feel that the class is getting too big, then find out if some variables seem to belong together and put them in separate classes. But don't make those classes global, but make objects of them accessible from your App class. So you have a tree like class structure with a single root.

Also do try to change all variables within a class only with memberfunctions of that class. For a start just make all variables private to ensure this. If another class need to access the variables, then create some access functions to do that.

There are some reasons to have more than one global variable, but this way of creating your application will get you a long way. Just always split your classes if they start to get too big.

Posted: Mon Jul 02, 2007 4:34 pm
by omar shaaban
well u should import global.h in both cpp files!! and i think that what u did :P