C++:为什么不编译?

Eam*_*orr 3 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;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

using ext::hash

'ext'尚未宣布

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

  • 'hash'未在此范围内声明
  • 模板参数3无效
  • 在','令牌之前预期的非限定id
  • '>'令牌之前的预期初始化程序

months.set_empty_key(NULL);

在此范围内未声明'月'

我有点像C++菜鸟,希望有人能指出我正确的方向.

提前谢谢了,

Kar*_*oor 5

也许你必试图取代ext::hashtr1::hash.

  • @Eamorr什么是关于`std :: tr1 :: hash`? (5认同)