C++:为什么不能编译?

标签 c++ hash compilation

我有以下从谷歌的 sparsehash 网站获得的 C++ 代码:

#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’ has not been declared

dense_hash_map<const char*, int, hash<const char*>, eqstr> months;

  • ‘hash’ was not declared in this scope
  • template argument 3 is invalid
  • expected unqualified-id before ‘,’ token
  • expected initializer before ‘>’ token

months.set_empty_key(NULL);

‘months’ was not declared in this scope

我是一个 C++ 菜鸟,希望有人能给我指出正确的方向。

非常感谢,

最佳答案

也许你应该尝试用 tr1::hash 替换 ext::hash

关于C++:为什么不能编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6873651/

相关文章:

c++ - 应用程序需要 OS X >= Lion

c++ - .NET c++ 中父级的子窗体调用函数

c++ - 可变大小和类型队列的类结构

java - 有没有办法让netbeans在编译时自动重启tomcat?

c++ - 如何强制重新运行cmake,或者通过CMakeLists.txt向makefile添加函数

ruby - 根据表示顺序的数组对散列进行排序

php - 多版本哈希密码

c++ - 如何在 Qt 中创建 MD5 哈希?

android - 警告 : API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'

android - 无法编译cwm恢复