1

I have one problem with namespases. It says "Multiple definition of phys1::x", why? Take a look at my code:

main.cpp

#include <cstdlib>
#include <iostream>
#include "abcd.h"
using namespace phys1;
using namespace std;
int main(){
    cout << "SD " << tits() << endl;
    system("pause");
    return 0;
}

abcd.h

#ifndef _ABCD_H_
#define _ABCD_H_
namespace phys1
{
    double xx = 9.36;
}
double tits();
#endif

abcd.cpp

#include "abcd.h"
double tits(){
    return phys1::xx;
}
rint
  • 255
  • 1
  • 3
  • 10
  • 1
    You're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Dec 14 '13 at 09:27
  • @chris: It's very common for childish programmers to use reserved identifiers just **because** they're reserved. It's like when telling to a kid "don't touch that". I've seen this abuse over and over even in well known libraries (and for no reason at all... `ABCD_H_INCLUDED` would look even better). – 6502 Dec 14 '13 at 09:30
  • @6502, I've seen tons of abuse in quite a few big libraries. I never understood why they did it. As you say, there are options within the rules that work. All this does, really, is put pressure on the implementors not to "screw up the library". – chris Dec 14 '13 at 09:34
  • @6502 To be fair, I haven't learned of this limitation until I've read the standard (and started frequenting SO ;-) ). Our C++ lecturer does research in compilers, co-authored several (experimental) C++ compilers, and still did not mention this in lectures (IIRR). I'd say at least some such "abuse" might be a simple case of not being known. – Angew is no longer proud of SO Dec 14 '13 at 09:43
  • 1
    I've only read "tits()" and chris' comment "You're using a reserved identifier" ... Then I saw `double tits`. Enough SO for today. – leemes Dec 14 '13 at 10:07

1 Answers1

5

double xx = 9.36; is a definition, and you can't define the same symbol across multiple translation units.

You can use a const, which gives the variable internal linkage, or a static:

//can't modify the value
const double xx = 9.36;

//can modify the value
//each translation unit has its own copy, so not a global
static double xx = 9.36;

or for a true global, extern:

extern double xx;

//cpp file
double xx = 9.36;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • But even if I dont define, but just declare like that 'double xx;' in namespace same error appears! How to handle that? – rint Dec 14 '13 at 09:30
  • @rint, It still has external linkage. It's almost equivalent to declaring the same global in different files. Both of them are visible to each other unless you give them internal linkage. – chris Dec 14 '13 at 09:32
  • @rint so I give you 3 alternatives, and you go ahead and take another one? gg – Luchian Grigore Dec 14 '13 at 09:36
  • @LuchianGrigore Thanks for the answer, now I have another problem, if I define class in namespace that error never appears! Why? – rint Dec 14 '13 at 09:46
  • 1
    @rint A class definition is different. The error has nothing to do with namespaces, you'd get it even if you remove the namespace. – Luchian Grigore Dec 14 '13 at 09:51