why can't global fn access global variable?
-
i've declared int iGlob in a header file. then get error: error C2039: 'iGlob' : is not a member of '`global namespace' for this code: void myGlob() { ::iGlob = 123; } why? :confused:
Did you extern the global variable in that .cpp file. The World is getting smaller and so are the people.
-
Did you extern the global variable in that .cpp file. The World is getting smaller and so are the people.
-
i've declared int iGlob in a header file. then get error: error C2039: 'iGlob' : is not a member of '`global namespace' for this code: void myGlob() { ::iGlob = 123; } why? :confused:
found the solution on CP's FAQ.... 6.2: How do I share a global variable among my .CPP files? (top) First, in one of your .CPP files (and only one) declare the variable at global scope (that is, outside of all function and class definitions). For example: int g_volume; Then in a header file that is included in all .CPP files - such as stdafx.h - add an extern declaration: extern int g_volume; The extern keyword tells the compiler that g_volume is an int declared in some other .CPP file. If you forget the first step, the linker will give an unresolved external error. :cool:
-
i tried extern "C" { #include "_Globals.h" } and extern int iGlob; in cpp file.. but no luck. i know this is probably trivial stuff, how about an example? .cpp file: ... .h file: ... Dlg file: ... thanx:cool:
i dont konw which compiler you are using but this works for me. ---in global.h---- extern int uGlobal; ---in global.cpp--- int uGlobal; ---place where you will need that variable--- #include "global.h" void somefunc() { uGlobal = 55; } all the files must be in the project, compiled and linked. Hope i got you rite and answeredit correctly. The World is getting smaller and so are the people.