Link to home
Start Free TrialLog in
Avatar of MDH
MDH

asked on

Global variable

I have a question about global variables in C++. I use Visual C++.

I have three different CMultiDocTemplate templates with a view(CFormView), doc(CDocument) and frame for each.

When I press on a button a variable should be incremented by one step. This variable should then be accessed and edited from all of the other windows.

Where should I declare the variable to keep it global and up to date during the program's lifetime?

As you understand I am quite the beginner, so please try to explain the answer. :-)

Thanks
Avatar of MDarling
MDarling

declare it in your main app file (this doesnt really matter where) and then wherever you use it in other files, declare it as extern

e.g. for an integer global variable called n...

myapp.cpp

int g_n=0;

mview.cpp, mydoc.cpp

extern int g_n;


hope this answers you question.
regards,
mike.
Avatar of MDH

ASKER

I tried that, but gen an error:

'Error LNK2001 Unresolved external symbol "int g_n"'
'fatal error LNK1120: 1 unresolved external'

I declared the int in the main .h file in the class declaration and tried to reference it in a viewclass-method with
'extern int g_n'

Could you please explain further?

Thanks
MDH,

MDarling's answer is correct.  There may still be problems in your program or you may have made a mistake in implimenting it, so in that case you should ask for additional help from MDH without rejecting the answer.  You should only reject and answer if the expert is really wrong.

Anyways, its hard to tell what is wrong, can you post some of the code that declares and uses this variable?
Hi MDH !!
   Keep u r variable in CMyApp class i.e u r Application Class.
class CMyApp
{
//some default generated by app wizard or added manually
public:
  (any datatype) m_MyVariable;
}
Whenever u want to refer it refer it by
((CMyApp *) AfxGetApp())->m_MyVariable
Avatar of MDH

ASKER

Hm.. Ok.. I will try to see what is wrong with the code.. If this is the way to do it I guess that it will work after a few tries :-)

MDarling - Could you please comment or answer this again and I will give you the points..

Thanks!
In one of the .cpp files you should define it:
  int g_n=0;

The extern keyword should only be used whith the declaration, not when defining or referencing it.
And yes, the Q was answered by MDarling.
Avatar of MDH

ASKER

Sorry, I already gave the points to MDarling..

Thanks for the help anyway
Hi MDH !!
   Why do want to have global variable instead have it Application class which will be effective till the application is running and also saves global memory. U should always avoid to have global variables
>> U should always avoid to have global
>> variables
It depends on your needs.  Sometimes there are very good reasons to have them.  And it doesn't really apply just to "true global" varaibles, it applies to any case where you've managed to create a design where an isntance of data exists only one time, like a class's static data member or a data member in a singleton.  These all have the same potential problems as global variables--and the same advantages.
ASKER CERTIFIED SOLUTION
Avatar of MDarling
MDarling

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
some confusion may have arisen from
my "it doesn't really matter where" statement.

i meant that you could just have
easily declared it in myview.cpp
as myapp.cpp

and then had externs in myapp.cpp
and mydoc.cpp

hope this clears up any confusion.

regards,
mike.
Avatar of MDH

ASKER

Of course this was it.. I included the .h file in several other files.. It worked just fine. In the one instance I want a global variable it is working..

Thanks for all the help everyone.