C++ map - expression must be an integral constant expression [duplicate]

被刻印的时光 ゝ 提交于 2019-12-12 06:07:18

问题


#include <map>
#include <string>

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

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

I can't work this one out...


回答1:


You need to place the code inside a function.

#include <map>
#include <string>

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

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.



来源:https://stackoverflow.com/questions/28263339/c-map-expression-must-be-an-integral-constant-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!