我正在尝试编译下面显示的C++代码,但是我得到一个错误说,
在src/LM.h中包含的文件:3:0,来自src/LM.cpp:1:src/common.h:30:13:错误:'hash'已在此范围内使用tr1 :: hash声明;
这是我用来编译下面文件的命令.
g ++ -std = c ++ 11 -Wall src/Foo.cpp
Foo.cpp中
#include "Foo.h"
...
Run Code Online (Sandbox Code Playgroud)
foo.h中
#ifndef FOO_H
#define FOO_H
#include "common.h"
//more code here
#endif
Run Code Online (Sandbox Code Playgroud)
COMMON.H
#ifndef _COMMON_H_
#define _COMMON_H_
#include <iostream>
#include <fstream>
#include <cmath>
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
#include <set>
#include <tr1/unordered_map>
#include <tr1/functional>
namespace std {
using tr1::unordered_map;
using tr1::hash;
} // namespace std
using namespace std;
//more code here
#endif
Run Code Online (Sandbox Code Playgroud)
我希望源代码使用std :: tr1 :: unordered_map和std :: tr1 :: hash而不是std :: unordered_map和std :: hash(实际上我正在对使用std :: tr1的分布式文件进行一些修改:: unordered_map和std :: tr1 :: hash).
我的代码可能有什么问题?
UPD:https: //github.com/clab/fast_align/blob/master/src/port.h似乎和我一样.但是,这个编译没有任何问题......有什么想法吗?
已经std::hash在C++ 11中了.你无法重新定义它.您可以使用其他名称tr1::hash.
可能最好的想法(如果你真的想使用std::tr1::hash/std::tr1::unordered_map而不是C++ 11结构)是编写你自己的命名空间,其中using你想要的所有结构都没有std::hash/std::unordered_map.
namespace common
{
using std::tr1::hash;
using std::tr1::unordered_map;
using std::vector;
// and so on
}
Run Code Online (Sandbox Code Playgroud)