簡體   English   中英

C ++錯誤:'unordered_map'沒有命名類型

[英]C++ error: 'unordered_map' does not name a type

據我所知,我正在做的一切正確,我收到了錯誤消息:

error: 'unordered_map' does not name a type
error: 'mymap' does not name a type

在我的代碼中,我有:

#include <unordered_map>

using namespace std;

//global variable
unordered_map<string,int> mymap;
mymap.reserve(7000);

void main {
  return;
}

我不知道這里可以找到什么....

編輯:我更新我的聲明時

std::tr1::unordered_map<string,int> mymap;

我能夠消除第一個錯誤,但是當我嘗試保留時,我仍然得到第二個錯誤消息。

EDIT2:正如下面所指出的,保留必須進入main,我需要用flag編譯

-std=c++0x

但是,仍然存在與unordered_map相關的錯誤,即:

error: 'class std::tr1::unordered_map<std::basic_string<char>, int>' has no member named 'reserve'

g++ -std=c++11編譯(我的gcc版本是gcc 4.7.2 )AND

#include <unordered_map>
#include <string>

using namespace std;

//global variable
unordered_map<string,int> mymap;

int main() {
  mymap.reserve(7000); // <-- try putting it here
  return 0;
}

你不能execute arbitrary expressions at global scope ,所以你應該放

mymap.reserve(7000);

在主要內部。

對於其他STL容器(如map和vector)也是如此。

如果要為早於c ++ 11的版本支持<unordered_map>
#include<tr1/unordered_map>並以下列形式聲明您的地圖: - std::tr1::unordered_map<type1, type2> mymap
這將使用技術報告1擴展以實現向后兼容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM