Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all,


I created a project called test, with a test.cpp and test.h file in it.
Now I want to declare a namespace like below:
namespace testname
{
int value;
void showvalue();
}


I want to put the declaration of this namespace into my test.h file.
In test.cpp, I implemented the showvalue() function (simply output the value of the variable "value") and try to assign a value to "value" variable.

void main()
{
testname::i = 5;
testname::showvalue();
}


But when I compile this project, I got an error saying that the variable "value" has already been defined in test.obj file.
Can anyone tell me why this happen and how I could place the namespace declaration into my test.h header file?

Any answers from you will be appreciated!
Posted
Updated 23-May-11 21:47pm
v2
Comments
Dalek Dave 24-May-11 3:47am    
Edited for Grammar, Spelling and Readability.

1 solution

This is not a name space problem; this is a linker problem.

It's not good to declare a global variable at all. Why not declaring a variable as a member of some class? You can make it static if you want.

Let's forget name spaces: they are irrelevant.
What happen in your case is this: your value object is not just declared; it is actually defined in header file; and you include the header file in two or more C++ files (CPP). The compilation units are actually only CPP files. Through include you're trying to create two global objects value: one is included in one CPP file, another in another CPP file, but the object value should be unique per executable image.

I do not recommend doing it at all. Global static variables are evil; you can always leave without them. But if you really need one, you need to make only one definition, but any number of declarations. Here is how:

Definition: in one CPP file only:
C++
int value;


Declaration, in one *.H file, which can be included in many other (but only different) files:
C++
extern int value;


You should also avoid multiple includes in the same CPP file. Add this to the beginning of all *.h files:

C++
#pragma once


Another possibility (pragma once is not portable) is using include guards:

C++
#ifndef __MY_UNIT_H
#define __MY_UNIT_H

#pragma once //can combine them
 
//all your include file contents here
 
#endif //__MY_UNIT_H


See http://en.wikipedia.org/wiki/Include_guard[^].


That will work.

Again, consider avoiding it. This is C++. Use classes for everything, avoid global objects.

—SA
 
Share this answer
 
v5
Comments
Legor 24-May-11 3:13am    
perfect!
Sergey Alexandrovich Kryukov 24-May-11 3:19am    
Thank you, Legor.
--SA
Dalek Dave 24-May-11 3:48am    
Good Answer, and good advice too.
Sergey Alexandrovich Kryukov 24-May-11 14:08pm    
Thank you, Dalek.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900