473,320 Members | 2,122 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,320 software developers and data experts.

A question about global variables

I am writing a program that contains multiple source and header files,
generally one of each for classes that are similar to each other. I
want to be able to declare an object of the class declared in a specific
header file, and include that header in the source file of a different
set of classes so that those classes will have access to that object. I
believe I'm going about this in the wrong way, any advice would be
appreciated. A demonstration follows:

/*main.cpp*/

#include "a.h"
#include "b.h"

int main() {
B myB;
return 0;
}

/*a.h*/

#ifndef A_H
#define A_H

class A {
public:
A();
};

A myA;

#endif

/*b.h*/

#ifndef B_H
#define B_H

class B {
public:
B();
};

#endif

/*a.cpp*/

#include "a.h"

A::A() { }

/*b.cpp*/

#include "b.h"
#include "a.h"

B::B() { A newA = A(myA); }

I get link errors when building this project:

..NET 2005:

a.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in
main.obj
b.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in
main.obj

Cygwin:

ken@ken-wn0vf73qmks ~/c
$ g++ main.cpp a.cpp b.cpp -o main -Wall
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccZtqXQj.o(.bss+0x0):a.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp:
first defined here
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccKtlZoq.o(.bss+0x0):b.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp:
first defined here
collect2: ld returned 1 exit status
Jul 23 '05 #1
2 3483

Ken Human wrote:
I am writing a program that contains multiple source and header files, generally one of each for classes that are similar to each other. I
want to be able to declare an object of the class declared in a specific header file, and include that header in the source file of a different set of classes so that those classes will have access to that object. I believe I'm going about this in the wrong way, any advice would be
appreciated. A demonstration follows:

/*main.cpp*/

#include "a.h"
#include "b.h"

int main() {
B myB;
return 0;
}

/*a.h*/

#ifndef A_H
#define A_H

class A {
public:
A();
};

A myA;
Ack! You almost NEVER want to do this. Every compilation unit that
includes this header will not create this variable, and that is the
error that you are getting.

Instead use extern A myA;

#endif

/*b.h*/

#ifndef B_H
#define B_H

class B {
public:
B();
};

#endif

/*a.cpp*/

#include "a.h"

A::A() { }
Now HERE you can declare your variable without problems.

A myA;


/*b.cpp*/

#include "b.h"
#include "a.h"

B::B() { A newA = A(myA); }

I get link errors when building this project:

.NET 2005:

a.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in main.obj
b.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in main.obj

Cygwin:

ken@ken-wn0vf73qmks ~/c
$ g++ main.cpp a.cpp b.cpp -o main -Wall
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccZtqXQj.o(.bss+0x0):a.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp: first defined here
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccKtlZoq.o(.bss+0x0):b.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp: first defined here
collect2: ld returned 1 exit status


Do these error make more sense now? Before, you included a.h in a.cpp,
main.cpp and b.cpp. As a result you declared A myA (indirectly via a.h)
THREE TIMES. The linker first registered this variable in main.o. When
it saw it AGAIN in a.cpp and AGAIN in b.cpp, it freaked out.

Don't feel embarassed, this is a very common mistake. I'd bet that
we've all done this before when we were learning the basics.

-Jason

Jul 23 '05 #2
Jason wrote:
[...]


Do these error make more sense now? Before, you included a.h in a.cpp,
main.cpp and b.cpp. As a result you declared A myA (indirectly via a.h)
THREE TIMES. The linker first registered this variable in main.o. When
it saw it AGAIN in a.cpp and AGAIN in b.cpp, it freaked out.

Don't feel embarassed, this is a very common mistake. I'd bet that
we've all done this before when we were learning the basics.

-Jason


Thanks for your explanation, Jason. I had tried using "extern A myA" in
my header file before but I was not aware that I had to also define it
in my source file. So my understanding of the extern keyword was lacking.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lian | last post by:
Hi all, I have installed a web-based software written in php which needs that i should turn "register_globals" from off to on in the php.ini. There are some comments for register_globals in...
1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
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...
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...
2
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
3
by: Ara Kooser | last post by:
Hello all, I hope I am posting this correctly. I am running Python 2.4.2 on Slackware 11.0 using IDLE. I am in the process of learning python so I am writing a text adventure game using...
17
by: kumar | last post by:
hi i want to know where & how the C variables gets stored i mean like volatile , pointer and string variables gets stored , whether it is on stack or some other places if is there any clear...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.