2011-07-29 87 views
3

我有下面的C++代码,这是我从谷歌的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

'分机' 尚未声明

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

  • '散' 在此范围未声明
  • 模板参数3是前无效
  • 预期不合格的ID '' 令牌
  • 预期初始化之前 '>' 令牌

and months.set_empty_key(NULL);

“月”在此范围

未声明我是一个有点C++小白和希望有人能指出我在正确的方向。

提前许多感谢,

+0

您是否包含ext库的头文件? #include 或其他一些库文件头丢失 – QuentinUK

回答

5

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

+0

试过...'tr1'没有被声明相同__gnu_cxx :: hash;( – Eamorr

+5

@Eamorr关于'std :: tr1 :: hash'有什么用? –

+1

工作原理!非常感谢!*刘海派对桌子*一切立即编译! – Eamorr

0

您是否尝试过通过__gnu_cxx更换分机::哈希::由评论所说散列或TR1 ::哈希?

+0

尝试过......'tr1'没有被声明为__gnu_cxx :: hash同样相同(很多感谢! – Eamorr