anu
anu

Reputation: 15

accessing map from a namespace

I got a namespace in map.h header file, and defined the map in map.cpp file as

map.h

    namespace mymap{
     .....
   }

map.cpp

   namespace mymap{
     static const map<int, string> mymap{
          {0, "zero"},
          {1, "one"}....
     }
   }

I want to access this map in another .cpp file called summary.cpp, when ever I do that the compiler throws an error mymap is not a not a member of mymap?

Why is this happening, how can I access that map in summary.cpp, I have included mymap.h in summary.cpp and using mymap::mymap to access it

Upvotes: 0

Views: 1042

Answers (1)

Deduplicator
Deduplicator

Reputation: 45674

  1. The keyword static used on a namespace-scope definition or declaration makes it translation-unit-local.
  2. const in the above situation would also imply static, unless overridden with extern.
  3. If you don't have a previous declaration for your symbol in a translation-unit (if not defined in that TU, it should come from a header which is also included first thing in the TU defining it, in order to detect mismatches), trying to use it is an error.

Upvotes: 0

Related Questions