While I agree that you should avoid global variables, like Santiago says, they can be needed at
times. The error you are getting is because each time you include the header you are in fact
redeclaring it. Headers are *only* for definitions, and no instantiation should occur in them. What
you need to do is create the global variable in the cpp file instead and in the header for that file
put an extern statement, i.e.
In the cpp file:
// global variable
int my_global_int;
In the .h file:
// allow other cpp files access to global variable
extern int my_global_int;
Hope this helps, Jason.
----- Original Message -----
From: "Daniel Glenfield" <sp...@ho...>
To: <dev...@li...>
Sent: Thursday, April 18, 2002 5:13 PM
Subject: [Dev-C++] Global variables (linking errors)
Help me out here would you? I have three C++ source files, each with its own
header file. I want to have a global variable that all three source files
can use.
But when I put the variable into one of the header files I get Linker
Errors, telling me the variable has already been defined in the first of my
source code files.
I think it's to do with the header files, but I'm not sure.
Can anyone help?
Daniel
_________________________________________________________________
Join the world's largest e-mail service with MSN Hotmail.
http://www.hotmail.com
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|