• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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
0
MDH
Asked:
MDH
  • 4
  • 3
  • 2
  • +2
1 Solution
 
MDarlingCommented:
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.
0
 
MDHAuthor Commented:
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
0
 
nietodCommented:
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?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
akalmaniCommented:
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
0
 
MDHAuthor Commented:
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!
0
 
KangaRooCommented:
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.
0
 
KangaRooCommented:
And yes, the Q was answered by MDarling.
0
 
MDHAuthor Commented:
Sorry, I already gave the points to MDarling..

Thanks for the help anyway
0
 
akalmaniCommented:
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
0
 
nietodCommented:
>> 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.
0
 
MDarlingCommented:
dont' declare the int in the .h file.

that may lead to multiple declarations due to multiple includes of the .h file.

declare it in the main.cpp file.

give it a go and let us know what your
compiler tells you.

regards,
mike.
0
 
MDarlingCommented:
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.
0
 
MDHAuthor Commented:
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.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now