Thread: compilation error using std::map

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    compilation error using std::map

    Can you tell me why the "netDefaults.insert(...)" line causes the C++ compilation errors shown below? I'm using Visual Studio 2003. The code shown below is enclosed in an anonymous namespace to restrict its scope to the local file. Everything compiles fine if I comment out the "netDefaults.insert(,,,)" line. I use the make_pair() macro elsewhere (with different typed arguments) and it works fine in those other places. Note that those other places are outside the anonymous namespace.

    Getting rid of the anonymous namespace makes no difference. Removing the const modifier on the netDefaults declaration makes no difference. Adding "const EnumPrefIndicator" to the TDefaultSettingsMap declaration makes no difference.

    Code:
      typedef struct _TPrefInfo
        {
        EnumSimpleType    dataType;
        void*             pDefaultValue;
    
        _TPrefInfo( EnumSimpleType type, void *pData ) : dataType( type ), pDefaultValue( pData ){};
        } TPrefInfo;
    
      typedef std::map< EnumPrefIndicator, TPrefInfo* > TDefaultSettingsMap;
    
      const TDefaultSettingsMap netDefaults;
    
      // Populate the default settings...
        // IPS.
    
    --> netDefaults.insert( make_pair( prefIpsStatusModeNipsStatus, new TPrefInfo( stBool, (void*)true ) ) );



    Prefs.cpp
    c:\MyDocs\Entercept\netprefs_new\NetPrefs\prefs\Pr efs.cpp(121) : error C2143: syntax error : missing ';' before '.'

    c:\MyDocs\Entercept\netprefs_new\NetPrefs\prefs\Pr efs.cpp(121) : error C2501: 'netDefaults' : missing storage-class or type specifiers

    c:\MyDocs\Entercept\netprefs_new\NetPrefs\prefs\Pr efs.cpp(121) : error C2373: '`anonymous-namespace'::netDefaults' : redefinition; different type modifiers

    c:\MyDocs\Entercept\netprefs_new\NetPrefs\prefs\Pr efs.cpp(114) : see declaration of '`anonymous-namespace'::netDefaults'

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You didn't specify the std namespace for make_pair.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    A "using namespace std;" declaration was in an included header file.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ewwww.... never use "using" statements in headers.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you sure EnumPrefIndicator, prefIpsStatusModeNipsStatus, stBool and EnumSimpleType are declared somewhere?

    Also, you shouldn't have the const there if you are calling insert on the map.

    If that doesn't help, maybe you could post more complete code.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Can you tell me why it's bad practice? Do your cautions apply even with a header guard in place?

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Like I said in the original post, all compiles cleanly if the insert(...) line is commented out. That means to me that all the other types, variables, etc are properly defined and available, correct?

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    On probably an unrelated note you can't make a map a const and then insert into it.

    Quote Originally Posted by dbeyer
    Can you tell me why it's bad practice? Do your cautions apply even with a header guard in place?
    The problem is that if someone else is using your class and include your header it will now break their code that is relying on the std namespace not being exposed.
    Last edited by Darryl; 03-02-2006 at 04:09 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> That means to me that all the other types, variables, etc are properly defined and available, correct?

    Probably, but not necessarily. Of course, I asked about prefIpsStatusModeNipsStatus and stBool, which in the code above are only used on the line that you commented out, meaning they could be the problem as well.

    Also check for a missing semicolon in the line above the one that gets the error.

    >> Can you tell me why it's bad practice? Do your cautions apply even with a header guard in place?

    Yes, the caution still applies. It is bad practice because it brings in the std namespace names to all files that include your header, as well as any file that includes a file that includes your header (and so on). Other files no longer have a choice. Putting a using directive in a source file is less bad, because only that source file will ever be affected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-20-2008, 12:31 PM
  2. Is std::map efficient for this problem?
    By dudeomanodude in forum C++ Programming
    Replies: 12
    Last Post: 04-10-2008, 02:15 PM
  3. std::map question.
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2008, 12:29 PM
  4. PC RAM in Compilation
    By SlyMaelstrom in forum Tech Board
    Replies: 5
    Last Post: 05-21-2006, 06:03 AM
  5. MS VC++ Crash on compilation
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-23-2003, 07:06 PM