简体   繁体   English

C ++无法使用{…}进行初始化

[英]C++ Can not initialize with {…}

I am using VS2012, and I wanted to include the following map: 我正在使用VS2012,并且想包含以下地图:

#include<iostream>
#include<map>

std::map<int, int> code_map = {
  {65, 97},
  {66, 98},
  {67, 99},
  {68, 100},
  {69, 101},
  {70, 102},  
};

However, the IDE tells me 但是,IDE告诉我

IntelliSense: Initialization with "{...}" invalid for object of type ""std::map, std::allocator>>"" IntelliSense:对于类型为““ std :: map,std :: allocator >>”“”的对象,使用“ {...}”初始化无效

Does anybody see why this error occurs? 有人知道为什么会发生此错误吗? I guess that I am using C++11 if I use VS2012, so I thought that would work. 我想如果我使用VS2012,我正在使用C ++ 11,所以我认为那可以工作。

Thank you for the help. 感谢您的帮助。

Visual Studio 2012 doesn't support initializer lists. Visual Studio 2012不支持初始化程序列表。 See this for the C++11 features it does support: http://msdn.microsoft.com/en-us/library/vstudio/hh567368%28v=vs.120%29.aspx 对于它支持的C ++ 11功能,请参见以下内容: http : //msdn.microsoft.com/zh-cn/library/vstudio/hh567368%28v=vs.120%29.aspx

Your code is perfectly fine according to C++11 standard. 根据C ++ 11标准,您的代码完全正确。

I remember reading somewhere that Microsoft C++ compiler haven't implemented uniform initialization and initializer lists Visual C++ Compiler November 2012 CTP release. 我记得在某处读到Microsoft C ++编译器尚未实现统一的初始化,而初始化程序列出了Visual C ++编译器2012年11月CTP版本。 Perhaps you just need to upgrade your compiler. 也许您只需要升级编译器。

There are examples to adding a map here: 这里有一些添加地图的示例:

http://www.cplusplus.com/reference/map/map/map/ http://www.cplusplus.com/reference/map/map/map/

I haven't used maps in c++ myself, but from what others have said, Visual Studio is a little slow at adding that functionality. 我自己没有在c ++中使用过地图,但是从其他人的意见来看,Visual Studio在添加该功能方面有些慢。

This link should help you do it the other way. 此链接应以其他方式帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM