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.
Where do I declare global variables?
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?
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?
It sounds like you just aren't defining the global variable. In any one of your .cpp files, add
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
Code: Select all
bool laser = false; // or true
Travis
OKWeng wrote:I followed the steps but there are still linker errors.
First I declared the variable in the Global.h file:
extern bool laser;
use:Weng wrote: Then I initialised the laser global variable in the .cpp file which contains the main() function:
laser = false;
bool laser = false;
I hope you included it in the necessary .cpp files.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.
Initialize it outside of all scopes.Weng wrote: What's wrong? :?
And also , where should I initialise the global variable?
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;
Code: Select all
App APP;
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.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact: