2011-07-29 84 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