Global variables.

C

calcop

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.
 
A

Alan Johnson

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
 
R

Roland Pibinger

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
 
Z

Zara

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.
 
I

Ian Collins

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top