'hash'已在此范围内使用tr1 :: hash声明;

时间:2014-10-28 10:12:00

标签: c++ c++11 tr1

我正在尝试编译下面显示的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"
...

foo.h中

#ifndef FOO_H
#define FOO_H
#include "common.h"
//more code here
#endif

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

我希望源代码使用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似乎和我一样。但是,这个编译没有任何问题...有任何想法吗?

1 个答案:

答案 0 :(得分:5)

C ++ 11中已经有std::hash。你无法重新定义它。您可以为tr1::hash使用其他名称。

可能最好的想法(如果你真的想使用std::tr1::hash/std::tr1::unordered_map而不是C ++ 11结构)是编写自己的命名空间,其中using所有结构,你想要的{{1} }}

std::hash/std::unordered_map