Paul
Paul

Reputation: 101

Errors with unordered_map in C++?

I was writing a program for a class using Visual C++ on my home computer, however, I tried to run it on school linux computers and I get these errors.

std::tr1::unordered_map <string, Word*> map;

Both of these errors appear on the line of code above

ISO C++ forbids declaration of ‘unordered_map’ with no type

expected ‘;’ before ‘<’ token

Originally I used hash_map but found out that could only be used in Visual C++

Thanks

Upvotes: 3

Views: 3633

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137860

GCC and MSVC define the TR1 extensions in different ways, because the TR1 standard is vague about how it should be supplied to the user. It simply specifies that there should be some compiler option to activate TR1.

Unlike MSVC, GCC puts the headers in a TR1 subdirectory. There are two ways to access them:

  1. Add a command-line option -isystem /usr/include/c++/<GCC version>/tr1. This is more conformant but appears to cause problems.
  2. Use conditional compilation:

    #ifdef __GNUC__
    #include <tr1/unordered_map>
    #else
    #include <unordered_map>
    #endif
    

    This exposes GCC's nonconformance: TR1 is not activated by setting an option, but instead by modifying the code.

    There is a somewhat esoteric way around this: computed header names.

    #ifdef __GNUC__
    #define TR1_HEADER(x) <tr1/x>
    #else
    #define TR1_HEADER(x) <x>
    #endif
    
    #include TR1_HEADER(unordered_map)
    

    This way, you only have to include things "once."

Upvotes: 3

Related Questions