开发者

C++: Why won't this compile?

I have the following C++ code which开发者_如何学运维 I got from google's sparsehash website:

#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;
}

I'm getting the following errors:

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

and months.set_empty_key(NULL);

‘months’ was not declared in this scope

I'm a bit of a C++ noob and was hoping someone could point me in the right direction.

Many thanks in advance,


Maybe thou shalt try to replace ext::hash by tr1::hash.


Have you tried to replace ext::hash by __gnu_cxx::hash or tr1::hash as suggested by the comment?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜