3

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

0

1 Answer 1

3

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."

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, see also stackoverflow.com/questions/5952602/…. Apparently there is a Boost.TR1 but it's not perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.