Definition of constant within namespace in cpp file

后端 未结 3 1387
青春惊慌失措
青春惊慌失措 2021-01-15 01:22

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

//Header.h
#include 
namespace MySpa         


        
相关标签:
3条回答
  • 2021-01-15 01:47

    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";
    
    0 讨论(0)
  • 2021-01-15 01:48

    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.

    0 讨论(0)
  • 2021-01-15 02:08

    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.

    0 讨论(0)
提交回复
热议问题