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

发布于 2024-11-27 03:10:38 字数 1768 浏览 0 评论 0 原文

我有以下从 google 的spareshash 网站获得的 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”尚未声明

dense_hash_map, eqstr>月;

  • “hash”未在此范围内声明
  • 模板参数 3 无效
  • “,”标记之前应有非限定 ID
  • 预期的初始值设定项位于“>”之前令牌

months.set_empty_key(NULL);

“months”未在此范围内声明

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

预先非常感谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-12-04 03:10:38

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

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

一杆小烟枪 2024-12-04 03:10:38

您是否尝试按照评论的建议将 ext::hash 替换为 __gnu_cxx::hash 或 tr1::hash ?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文