Search code examples
c++stlinitializationglobal-variablesstatic-initializer

populating map globally


I have declared the following map globally and trying to populate globally.

   1: typedef std::map<unsigned short,std::pair<char,std::string>> DeviceTypeList;
   2: DeviceTypeList g_DeviceTypeList;
   3: g_DeviceTypeList.insert( std::make_pair ((unsigned short)SINGLE_CELL_CAMERA,
   std::make_pair('B',"Single Cell Camera")));

it is showing error like error C2143: syntax error : missing ';' before '.' at line2.

1 Am I doing something wrong
2. why can't we initialize the map globally.


Solution

  • Compiler is probably getting confused by the >> on line 1 (because it looks like a shift operator). Try inserting a space in there:

    typedef std::map<unsigned short,std::pair<char,std::string> > DeviceTypeList;
    

    [update]

    See Vlad Lazarenko's comment for why this will not actually solve your problem. Easiest fix is to wrap this contraption in an object, initialize it in the constructor, then declare one at global scope. (But not if you can avoid it since globals are evil in the first place...)