【问题标题】:C++ Standard Template Library simple code giving error in vs code in macos mojaveC++ 标准模板库简单代码在 macos mojave 中的 vs 代码中给出错误
【发布时间】:2020-08-31 21:35:44
【问题描述】:

我在 vs 代码编辑器中使用 C++ 中的 STL 工作,并且我已经声明了一个数组,如下面的代码中给出的......

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    vector<int> arr={11,2,13};
    for(int i=0;i<3;++i)
    cout<<arr[i]<<" ";
    return 0;
}

我收到无法使用初始化列表初始化非聚合类型向量的错误。 当我在 Clion ide 中编译代码时,它工作正常。 那么vs code有什么问题呢?

【问题讨论】:

  • Visual Studio 代码只是一个 IDE,您使用的是哪个编译器以及您传递给它的参数是什么
  • 我有一个运行 macos mojave 10.14.5 的 macbook air 2017,我正在使用 xcode 附带的编译器
  • 我也用过vs code的c++扩展在vs code中写c++代码
  • 我使用的是 xcode 自带的编译器 -- 那是什么编译器?另外,您在 VS Code 中使用的是什么编译器?您需要了解实际用于构建程序的底层工具。

标签: c++ templates stl linker-errors


【解决方案1】:

编译器好像不支持c++11。

您可以使用-std=c++11 再试一次。

例如。 g++ -std=c++11 -c example.cpp

也可以通过why am I getting “non-aggregate cannot be initialized with initializer list获取更多信息

【讨论】:

  • 是的,当我使用任何 c++ 11 功能时,代码编辑器会警告我此功能是 c++ 11 的一部分...
  • 有没有办法直接将其设置为 c++ 14 for always in vs code?​​span>
  • @DhruvBansal 也许您需要使用 C++ 14 功能设置编译命令。很抱歉,我没有更多的 vs 代码经验。
猜你喜欢
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
相关资源
最近更新 更多