3

I've approached this strange ( for me ) effect in VS 2010. Can anyone smart shed some light on it please.

//Header.h
#include <string>
namespace MySpace {
extern const std::string SOME_CONST_STRING;
}

//Implementation.cpp
#include "Header.h"
using namespace MySpace;
const std::string SOME_CONST_STRING = "CONST_STRING_VALUE";

This causes linker to output error LNK2001: unresolved external symbol const MySpace::SOME_CONST_STRING.

However when I change Implementation.cpp like this:

//Implementation.cpp
#include "Header.h"
namespace MySpace {
const std::string SOME_CONST_STRING = "CONST_STRING_VALUE";    
}

the code builds OK.

Is it good example for prefering defining of namespace in cpp file rather than using it ?

jakumate
  • 115
  • 8
  • I don't understand your last question, I'm afraid. – Joseph Mansfield Jul 11 '14 at 12:55
  • SOME_CONST_STRING in cpp also needs extern. – Neil Kirk Jul 11 '14 at 12:59
  • The only difference in two versions of Implementation.cpp is using namespace MySpace; vs namespace MySpace { ... }. First one fails to link, while second links OK. – jakumate Jul 11 '14 at 13:08
  • Joseph. I saw it in "coding style" questions: what is prefered way of writing it in cpp files: using namespace MySpace; vs namespace MySpace { ... }. There were few examples which one is better over the other. I'm asking if the problem above is good reason to not write using namespace MySpace; in implementation cpp files ? – jakumate Jul 11 '14 at 13:12

3 Answers3

0

You can alternatively write:

const std::string MySpace::SOME_CONST_STRING = "CONST_STRING_VALUE";

That name is declared inside MySpace so you have to refer to it as such.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

You are missing "namespace" in your using. It should be like this:

#include "Header.h"
using namespace MySpace;
const std::string SOME_CONST_STRING = "CONST_STRING_VALUE";
Johan Hjalmarsson
  • 3,433
  • 4
  • 39
  • 64
  • Thanks Johan this is just a "typo" in the question. I'll edit it. – jakumate Jul 11 '14 at 12:57
  • SOME_CONST_STRING in cpp also needs extern. – Neil Kirk Jul 11 '14 at 13:01
  • Also I'm pretty sure your code doesn't work. How does it know SOME_CONST_STRING is in MySpace and not the global namespace? – Neil Kirk Jul 11 '14 at 13:01
  • No it doesn't, "extern" in header is sufficent. That part is OK. ;) – jakumate Jul 11 '14 at 13:03
  • The only difference in two versions of Implementation.cpp is using namespace MySpace; vs namespace MySpace { ... }. First one fails to link, while second links OK. – jakumate Jul 11 '14 at 13:07
  • @jakumate ok I've asked question here to find more info: http://stackoverflow.com/questions/24698515/is-extern-keyword-required-here-const-in-cpp-file – Neil Kirk Jul 11 '14 at 13:08
0

The whole point of namespaces is that names in a particular namespace are independent of names outside that namespace. A program could have both a MySpace::SOME_CONST_STRING and a global ::SOME_CONST_STRING, and these are two completely unrelated symbols. It would be wrong for the linker to use a definition of one to satisfy a reference to the other.

When you define a variable, you have to define it in the namespace where you want it to be.

Wyzard
  • 33,849
  • 3
  • 67
  • 87