开发者

'hash_map' was not declared in this scope with g++ 4.2.1

开发者 https://www.devze.com 2022-12-16 21:48 出处:网络
I am trying to use sgi hash_map. #include <list> #include <iostream> #include <string>

I am trying to use sgi hash_map.

#include <list>
#include <iostream>
#include <string>
#include <map>
#include <cstring>
#include <tr1/unordered_map>
#include <ext/hash_map>


using namespace std;
struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};


int main()
{
  hash_map<const char*, int, hash<const char*>, eqstr> months;

  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;

  cout << "september -> " << months["september"] << endl;
  cout << "april     -> " << months["april"] << endl;
  cout << "june      -> " << months["june"] << endl;
  cout << "november  -> " << months["november"] << endl;
} 

on gcc4.2 I am getting the error

listcheck.cc: In function 'int main()':
listcheck.cc:22: error: 'hash_map' was not declared in this开发者_运维技巧 scope
listcheck.cc:22: error: expected primary-expression before 'const'
listcheck.cc:22: error: expected `;' before 'const'
listcheck.cc:24: error: 'months' was not declared in this scope

while the same code compile with 3.4.


Use <unordered_map>. hash_map was a vendor specific extension, replaced by unordered_map.


The include file <ext/hash_map> refers to the GNU extension hash map class and this is declared in namespace __gnu_cxx. You can either explicitly qualify the template name or add:

using namespace __gnu_cxx;


using namespace __gnu_cxx; removed the compilation error.

using

#include <hash_map>

gives this warning and removing gives a compilation error

In file included from /usr/include/c++/4.4/backward/hash_map:59,
                 from listcheck.cc:6:
/usr/include/c++/4.4/backward/backward_warning.h:28:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated.

after removing

#include <hash_map>

 g++ listcheck.cc
listcheck.cc: In function ‘int main()’:
listcheck.cc:20: error: ‘hash_map’ was not declared in this scope
listcheck.cc:20: error: expected primary-expression before ‘const’
listcheck.cc:20: error: expected ‘;’ before ‘const’
listcheck.cc:21: error: ‘months’ was not declared in this scope
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号