C++中unordered_map出现错误?

3

我在家用Visual C++写了一个课程作业程序,但是当我尝试在学校的Linux电脑上运行它时,出现了以下错误。

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

这两个错误都出现在上一行代码上。

ISO C++不允许声明未知类型的‘unordered_map’。

在‘<’标记之前缺少‘;’。

最初我使用了hash_map,但发现它只能在Visual C++中使用。

谢谢。

1个回答

3

GCC和MSVC以不同的方式定义TR1扩展,因为TR1标准对如何向用户提供它并没有明确规定。它只指定应该有一些编译器选项来激活TR1。

与MSVC不同,GCC将头文件放在TR1子目录中。有两种方法可以访问它们:

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


啊,参见https://dev59.com/oG025IYBdhLWcg3whWdm。显然有一个Boost.TR1,但它并不完美。 - Potatoswatter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接