【问题标题】:Compile time error while using unordered_map使用 unordered_map 时出现编译时错误
【发布时间】:2013-04-04 17:26:51
【问题描述】:

我得到了下面的示例代码

// unordered_map::find
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_map<std::string,double> mymap = {
     {"mom",5.4},
     {"dad",6.1},
     {"bro",5.9} };

  std::string input;
  std::cout << "who? ";
  getline (std::cin,input);

  std::unordered_map<std::string,double>::const_iterator got = mymap.find (input);

  if ( got == mymap.end() )
    std::cout << "not found";
  else
    std::cout << got->first << " is " << got->second;

  std::cout << std::endl;

  return 0;

当我尝试在 Windows 7 上使用 VS 2010 编译它时,我得到了编译时错误(虽然我看起来没问题)

1>\testing.cpp(13): error C2552: 'mymap' : non-aggregates cannot be initialized with initializer list
1>          'std::tr1::unordered_map<_Kty,_Ty>' : Types with a base are not aggregate
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=double
1>          ]
1>\testing.cpp(14): error C2078: too many initializers
1>\testing.cpp(15): fatal error C1903: unable to recover from previous error(s); stopping compilation

【问题讨论】:

    标签: c++ stl unordered-map


    【解决方案1】:

    您的编译器 (VC10) 不支持统一初始化。你的程序compiles fine on a conforming compiler

    【讨论】:

    • 请注意,到目前为止,没有任何版本的 VC 具有库初始值设定项列表构造函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2013-06-16
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多