【问题标题】:'hash_map' was not declared in this scope with g++ 4.2.1'hash_map' 未在此范围内使用 g++ 4.2.1 声明
【发布时间】:2010-01-18 04:54:20
【问题描述】:

我正在尝试使用 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;
} 

在 gcc4.2 上出现错误

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

同样的代码用 3.4 编译。

【问题讨论】:

    标签: c++ sgi


    【解决方案1】:

    使用&lt;unordered_map&gt;。 hash_map 是供应商特定的扩展,被 unordered_map 取代。

    【讨论】:

      【解决方案2】:

      包含文件&lt;ext/hash_map&gt; 引用GNU extension 哈希映射类,它在命名空间__gnu_cxx 中声明。您可以明确限定模板名称或添加:

      using namespace __gnu_cxx;
      

      【讨论】:

        【解决方案3】:

        使用命名空间 __gnu_cxx;删除了编译错误。

        使用

        #include <hash_map>
        

        给出这个警告,删除给出一个编译错误

        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.
        

        删除后

        #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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多