473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2407
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.