C ++ map - expression必须是一个整数常量表达式 [英] C++ map - expression must be an integral constant expression

查看:515
本文介绍了C ++ map - expression必须是一个整数常量表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <map>
#include <string>

std::map<std::string, int> foo;
foo["bar"] = 1;

为什么在visual studio 12中得到错误expression must be an integral constant expression

Why do I get the error "expression must be an integral constant expression" in visual studio 12?

我无法使用这个工具...

I can't work this one out...

推荐答案

您需要将代码放在函数内。

You need to place the code inside a function.

#include <map>
#include <string>

void xyz()
{
   std::map<std::string, int> foo;
   foo["bar"] = 1;
}

我验证了VS 2013有一个问题, 。正如其他人所指出的,大多数语句不允许在函数之外。

I verified VS 2013 has a problem otherwise, but it works when inside a function. As others have noted, most statements aren't allowed outside of a function.

这篇关于C ++ map - expression必须是一个整数常量表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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