我正在尝试静态地初始化一个包含一对的map:
typedef map<int, pair<int, int>> mytype;
static const mytype mymap = { 3, {3, 0} };我使用的是Visual Studio 2013,但出现错误:
error C2440: 'initializing' : cannot convert from 'initializer-list' to 'std::map<int,std::pair<int,int>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'你知道这是什么原因吗?我以为VS2013有这个C++11功能。
发布于 2015-03-09 00:53:21
你少了一组花括号:
static const mytype mymap = { { 3, {3, 0} } };
^ ^ ^
| | pair<int,int> (value)
| pair<const key, value> (map element)
map<key, value>发布于 2015-03-09 00:54:28
编译器认为你想用两个元素初始化map。
正确的语法应该是:
static const mytype mymap = { { 3, {3, 0} } };https://stackoverflow.com/questions/28929113
复制相似问题