C++:为什么这个代码无法编译?

3

我有以下的 C++ 代码,这些代码来自 Google 的 sparsehash 网站:

#include <iostream>
#include <google/dense_hash_map>
#include <string.h>

using google::dense_hash_map;      // namespace where class lives by default
using std::cout;
using std::endl;
using ext::hash;  // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
  }
};

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

  months.set_empty_key(NULL);
  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;
}

我遇到了以下错误: using ext::hash

‘ext’未声明

dense_hash_map<const char*, int, hash<const char*>, eqstr> months;
  • ‘hash’在此作用域中未声明
  • 第3个模板参数无效
  • 在逗号标记之前需要非限定的id
  • 在 ‘>’ 标记之前需要初始化器
还有 months.set_empty_key(NULL);

‘months’在此作用域中未声明

我是一个C++新手,希望有人能指点我正确的方向。
非常感谢。

你是否已经包含了ext库的头文件?例如#include <ext/hash>或者其他库的头文件缺失。 - QuentinUK
2个回答

5
也许你可以尝试用tr1::hash替换ext::hash

尝试过了... 'tr1' 未声明。__gnu_cxx::hash 同理 ;( - Eamorr
5
@Eamorr std::tr1::hash是什么? - Karl von Moor
1
太好了!非常感谢!猛敲桌子 现在所有代码都可以编译了! - Eamorr

0
你尝试过按照注释建议,将 ext::hash 替换为 __gnu_cxx::hash 或 tr1::hash 吗?

尝试过了... 'tr1' 未声明。__gnu_cxx::hash 也是一样的情况 ;( 非常感谢! - Eamorr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接