2012-11-03 90 views
4

我正在爲我的C++編程類實現散列映射。我的教師給了我們一個頭文件,我們需要使用我們的哈希映射類。提供的頭文件包含以下行:C++編譯錯誤 - 「命名空間標識中沒有類型命名'函數'

typedef std::function<unsigned int(const std::string&)> HashFunction; 

從我對C++的理解中,可以將HashFunction類型定義爲std :: function。然而,當我編譯代碼,我得到的錯誤:

./HashMap.h:46:15: error: no type named 'function' in namespace 'std' 
     typedef std::function<unsigned int(const std::string&)> HashFunction; 
       ~~~~~^ 
./HashMap.h:46:23: error: expected member name or ';' after declaration specifiers 
     typedef std::function<unsigned int(const std::string&)> HashFunction; 
     ~~~~~~~~~~~~~~~~~~~~~^ 

的HashMap.h文件有

#include <functional> 

在頂部,如果它很重要。

有誰知道我爲什麼會得到這些錯誤?

+1

這是C++ 11,這樣有可能有所作爲。 – chris

+1

當編譯器支持仍然不完善時(至少需要一些專業知識才能找到支持您需要的支持的實現),才能看到一所學校在其作業中需要C++ 11。看起來它可能已經倒轉...... –

回答

6

您需要一個具有(至少部分)C++ 11支持的編譯器。你正在使用哪種編譯器?

+0

我正在使用GCC 4.2,它顯然沒有C++ 11的支持。我切換到Visual Studio 2012,現在它可以工作。非常感謝! –

+1

輸出看起來更像是Clang而不是GCC。你確定你在使用GCC嗎? –

+1

@NathanOsman:GCC最近的4.x版本現在有一個類似於Clang的輸出。 – greyfade

0

只需添加:

 
CONFIG +=c++11 

到.pro文件:)

相關問題