Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 7th, 2006, 02:15 AM
razael1@gmail.com
Guest
 
Posts: n/a
Default Global variables in include file

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles