470,841 Members | 1,037 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

Global variables.

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.

Jan 16 '06 #1
6 2212
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.


Variables should not be defined in header files. Pick one of your cpp
files (or make a new one, for example, strings.cpp) and define it there.
In your header file it should be declared (not defined) with the
extern keyword. For example:

extern char sysName[32] ;

-Alan
Jan 16 '06 #2
thank you

Jan 16 '06 #3
On Mon, 16 Jan 2006 00:27:45 -0800, Alan Johnson
<al****@stanford.dot.nospam_edu> wrote:

Variables should not be defined in header files. Pick one of your cpp
files (or make a new one, for example, strings.cpp) and define it there.
In your header file it should be declared (not defined) with the
extern keyword. For example:

extern char sysName[32] ;


Better avoid any global variables and pass argumnents to functions.

Best wishes,
Roland Pibinger
Jan 16 '06 #4
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.
Jan 16 '06 #5
Thanks everyone, Alan was right on the money and it worked. Thanks
again.

Jan 16 '06 #6
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?

One way is to declare sysName (as a string) as extern in the header and
define it in one of your .cpp files.

strings.h:

extern std::string sysName;

main.cpp:

std::string sysName;

--
Ian Collins.
Jan 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Matt | last post: by
4 posts views Thread by Andrew V. Romero | last post: by
12 posts views Thread by David WOO | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
5 posts views Thread by Sandman | last post: by
112 posts views Thread by istillshine | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.