C ++错误:"unordered_map"未命名类型 [英] C++ error: 'unordered_map' does not name a type

查看:277
本文介绍了C ++错误:"unordered_map"未命名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我正在正确地做所有事情,并且得到了错误消息:

I am doing everything correctly as far as I can tell and I have gotten the error message:

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;
}

我看不出这里可能缺少什么....

I don't see what can be missing here....

当我将声明更新为

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

我能够消除第一个错误,但是当我尝试保留时,仍然会收到第二个错误消息.

I an able to eliminate the first error, but when I try to reserve, I still get the second error message.

正如下面所指出的,储备金必须进入main且我需要用标志

As pointed out below, reserve must go into main and I need to compile with flag

-std=c++0x

但是,似乎仍然存在与unordered_map相关的错误,即:

However, there still appear to be errors related to unordered_map, namely:

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 )和

Compile with g++ -std=c++11 (my gcc version is 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;
}

这篇关于C ++错误:"unordered_map"未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆