Connecting Tech Pros Worldwide Help | Site Map

Global variables in include file

  #1  
Old December 7th, 2006, 02:15 AM
razael1@gmail.com
Guest
 
Posts: n/a
Hi,

I am declaring a global variable in a .h file like this:

extern MyClass object;

Then I include the .h file in other files and try to use object. I get
"object not declared", but then when I add:

MyClass object;

to the file I am including the .h file in, I get "multiple definitions
of object". How can I fix this?

Thanks,
Colin

  #2  
Old December 7th, 2006, 02:25 AM
Joseph Paterson
Guest
 
Posts: n/a

re: Global variables in include file


You shouldn't define global variables in header files, you should only
declare them there.
For example, in myFile.cpp you would have:

int myGlobal = 1;

and in myFile.h you would have:

extern int myGlobal;

now all files that include myFile.h will have access to the global
variable myGlobal.

Joseph.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Global variables in Shared Libraries in Linux nidumolu@gmail.com answers 1 September 4th, 2006 11:35 PM
Global variables in Shared Libraries in Linux nidumolu@gmail.com answers 0 September 4th, 2006 11:05 PM
using global variables in the database connection string frothpoker answers 3 June 20th, 2006 11:25 PM
Global variables in files without functions zeecanvas@yahoo.com answers 7 March 17th, 2006 02:35 AM