我在家用Visual C++写了一个课程作业程序,但是当我尝试在学校的Linux电脑上运行它时,出现了以下错误。
std::tr1::unordered_map <string, Word*> map;
这两个错误都出现在上一行代码上。
ISO C++不允许声明未知类型的‘unordered_map’。
在‘<’标记之前缺少‘;’。
最初我使用了hash_map,但发现它只能在Visual C++中使用。
谢谢。
我在家用Visual C++写了一个课程作业程序,但是当我尝试在学校的Linux电脑上运行它时,出现了以下错误。
std::tr1::unordered_map <string, Word*> map;
这两个错误都出现在上一行代码上。
ISO C++不允许声明未知类型的‘unordered_map’。
在‘<’标记之前缺少‘;’。
最初我使用了hash_map,但发现它只能在Visual C++中使用。
谢谢。
GCC和MSVC以不同的方式定义TR1扩展,因为TR1标准对如何向用户提供它并没有明确规定。它只指定应该有一些编译器选项来激活TR1。
与MSVC不同,GCC将头文件放在TR1
子目录中。有两种方法可以访问它们:
-isystem /usr/include/c++/<GCC version>/tr1
. This is more conformant but appears to cause problems.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."