0

I have a link error on the following code.

header file

    //we have a static class member std::map that I want to be read-only
    //the enums are numeric but not sequential
    typedef std::map<MyEnumType, std::string> MyStdMapType

    class MyClass { static const MyStdMapType myMap; }
    

source file

    //populate - static var outside of class
    const MyStdMapType MyClass::myMap = { {enumVal1, str1}, ... }
    

Then, later, I want to iterate through the map

      for (MyStdMapType::const_iterator iter = MyClass::myMap.begin(); iter != MyClass::myMap.end(); iter++)
      {
<stuff>
      }

The "for" line is giving a link error. If I comment the line out, it compiles!:

error LNK2001: unresolved external symbol "public: static class std::map<enum MyEnumType,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<enum MyEnumType>,class std::allocator<struct std::pair<enum MyEnumType const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > const MyClass::myMap" (?myMap@MyClass@@2V?$map@W4MyEnum@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$less@W4MyEnum@@@3@V?$allocator@U?$pair@$$CBW4MyEnum@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@3@@std@@B)
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Essentially, you need to include `const MyStdMapType myMap;` in a source file (not a header) somewhere. – 1201ProgramAlarm Jul 01 '20 at 21:20
  • Brethren: Points taken - agreed. My apologies. I've updated the post. To summarize: 1. typedef defined in header. Static class variable declared in class in header. 2. Relevant class variable defined and initialized in cpp file. I'm aware [through bitter experience!] of the intricacies of setting up static variables... Rgds Steve – Stephen Baldwin Jul 02 '20 at 00:50
  • How can this be a duplicate if he is assigning a value to the static variable in a source file? – Jerry Jeremiah Jul 02 '20 at 01:02
  • More details: I have other static vars set up, and they work fine. If I move the iterative code into the same class, it compiles fine. The problem occurs when I'm accessing the [public static] variable from another class in another cpp, #including the relevant header. Rgds Steve – Stephen Baldwin Jul 02 '20 at 01:25
  • I can further confirm that when the static is accessed from within its own class [in the constructor], the iteration works perfectly, and the correct enum key and std::string value pairs are debug printed. Per @Jerry Jeremiah, is this a duplicate? – Stephen Baldwin Jul 02 '20 at 02:46
  • 1
    I have voted to reopen this as I don't think it is a duplicate. – Jerry Jeremiah Jul 02 '20 at 04:05
  • The user who closed my question as duplicate pointed me to another that does not address my issue. However, I think it is related to this one: Accessing a global static variable from another class https://stackoverflow.com/questions/5540091/accessing-a-global-static-variable-from-another-class /Steve – Stephen Baldwin Jul 02 '20 at 15:07

0 Answers0