On 15 Jan 2006 23:52:42 -0800,
ca****@gmail.com wrote:
Hello everyone,
I hope I can write this question clearly. Anyway, it is about global
variables. I have several files in my C++ project. One of which is
main.cpp. This file comtains my main() function. I have a few other
files in my project. They are server.cpp, server.h, strings.h,
includes.h and functions.h. My main.cpp function #includes the
includes.h file which includes strings.h and functions.h.
My main() function fills a variable located win strings.h called
sysName[32]. My main() function then calls a function in server.cpp.
Becuase I delcared a prototype in server.h, it works. Now, my function
in server.cpp cannot read my sysName variable even though #include
"includes.h" is located in its header file.
Why can't I access this variable. I think all the #includes are
correct?
I have been searching google and have been working on this for like 2
hours now with no luck.
Thanks for any help.
You have submitted incomplete information...
But, anycase, I think there may be two possibilities:
a) In strings. h you have a declaration like "extern char
sysName[32];", but you have not defined sysyName anywhere. Thus, you
will have a linker error.
b) (MOST PROBABLE) In strings.h you have a definition like "char
sysyName[32];". This way, you will have a global sysName for each
unit that includes string.h. The linekr may (or may not) giove some
warning about duplicate global name, but the syysName seen from main
and from server are different ones, so they will not communicate at
all.
Solution, in both cases:
In strings.h, declare the variable: extern char sysName[32];
Create and include in the project, a module strings.cpp, with the
definition: char sysnName[32];
Then all modules will refer to the same sysName
If your problem is not one of those outlines above, please submit
significant pieces of code to identify the real problem.
Best regards,
Zara
PS: Someone in this list mayl flame you for using global variables
instead of, for instance, a Singleton class or something alike. The
solution I give is intende as the fastest to implement, not the mos
elegant/smartest one.