Reputation: 1068
I am trying to compile C++ code shown below but I got an error saying,
In file included from src/LM.h:3:0, from src/LM.cpp:1: src/common.h:30:13: error: ‘hash’ is already declared in this scope using tr1::hash;
This is the command I used to compile the files below.
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
I want the source code to use std::tr1::unordered_map and std::tr1::hash rather than std::unordered_map and std::hash(Actually I am making some modifications to distributed files which does uses std::tr1::unordered_map and std::tr1::hash).
What is possibly wrong with my codes?
UPD: https://github.com/clab/fast_align/blob/master/src/port.h seems to do the same thing as mine. However, this compiles without any problem... Have any idea?
Upvotes: 1
Views: 2501
Reputation: 55897
There is already std::hash
in C++11. You cannot redefine it. You can use another name for tr1::hash
.
Probably the best idea (if you really want to use std::tr1::hash/std::tr1::unordered_map
instead of C++11 structures) is to write your own namespace in which using
all structures, that you want without std::hash/std::unordered_map
.
namespace common
{
using std::tr1::hash;
using std::tr1::unordered_map;
using std::vector;
// and so on
}
Upvotes: 5