【问题标题】:Unable to initialize vector with initializer list in visual studio code无法在 Visual Studio 代码中使用初始化列表初始化向量
【发布时间】:2021-07-09 23:45:14
【问题描述】:
#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int> v = {1,2,3,4};
    for (int x : v)
    {
        cout << x << ' ';
    }
return 0;
}

当我在 vscode 中运行上述代码时,我收到以下错误:

不能用初始化列表 gcc [7, 17] 初始化非聚合类型“向量”

注意 - 错误包括 gcc,即使那不是我使用的编译器。

代码在终端和 Xcode 中编译得很好,所以我知道它与 vscode 有关。我该如何解决这个问题?

注意 - 我正在使用具有以下配置的 I C/C++ IntelliSense:编译器路径 (/usr/bin/clang++) IntelliSense 模式 (macros-clang-arm64) 包含路径 (${workspaceFolder} /**) C 标准 (c17) C++ 标准 (C++17)。

【问题讨论】:

  • 您是否启用了 c++11 或更高版本?
  • 如何检查我是否在 VSCode 中使用 C++11 或更高版本?我一直在网上搜索一个清晰简洁的答案,但无济于事。
  • 这可能是默认设置已经融入您的项目的结果,您使用的是 platformio 吗?您可能需要升级它或修复它的配置文件。
  • 您的代码在最新的 gcc 中运行良好:gcc.godbolt.org/z/nqcvsecd6
  • 我知道代码在在线编译器上运行良好。它在我的终端中也可以正常工作。这个问题介于 MacOS 和 VSCode 之间。

标签: c++ visual-studio-code vector initialization


【解决方案1】:

我复制了您的代码并将其命名为test.cpp。我遇到了同样的问题,我通过添加一些配置解决了这个问题。找到tasks.json 并在args 中添加一些内容。

"args": [
    "-g",
    "-Wall",
    "-std=c++11",
    "test.cpp"
]

它适用于我的 MAC!我使用command+shift+B进行编译,编译后生成a.out。然后你可以通过F5 运行它。我还在这里发布了我的launch.json,其中/Users/work/Foo 是我的工作区文件夹。注意program这行,我改了这行。祝你好运!

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C/C++ Runner: Debug Session",
      "type": "cppdbg",
      "request": "launch",
      "args": [],
      "stopAtEntry": false,
      "cwd": "/Users/work/Foo",
      "environment": [],
      "program": "/Users/work/Foo/a.out",
      "MIMode": "lldb",
      "externalConsole": true
    }
  ]
}

【讨论】:

    【解决方案2】:

    您可以发布您的代码以及如何编译它。以下对我有用:

    #include <vector>
    #include <iostream>
    
    int main() {
        std::vector<int> v = {1,2,3,4};
        for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) {
            std::cout << *i << ' ';
        }
        std::cout << std::endl;
        return 0;
    }
    

    编译运行如下:

    $ g++ -std=c++11 test.cpp
    $ ./a.out
    1 2 3 4
    $
    

    【讨论】:

    • 我复制并粘贴了您的代码并将其保存到 test.cpp 文件中,然后在终端中运行相同的两个命令: $ g++ -std=c++11 test.cpp $ ./a .out 然后收到相同的输出:1 2 3 4 但是,当我在 vscode 中运行 test.cpp 时,我收到以下错误:“非聚合类型 'std::vector' 无法使用初始化列表进行初始化”为什么是这样?我的配置如下: Compiler Path = /usr/bin/g++ IntelliSense Mode = macos-gcc-x64 C++ standard = c++17
    • 老实说,我不太了解 vscode 或如何配置它来编译 C++ 代码。也许这个答案将有助于设置标志:stackoverflow.com/a/57173879/19410 我希望我能提供帮助。您可以使用这些详细信息编辑您的问题,我将投票重新打开。或者提出一个新问题?
    • 我编辑了这个问题。重新提出问题的投票将有很大帮助。由于前两次我问错了问题,我无法再问另一个问题。
    • 如果我们有范围的 for 循环,为什么要写完整的 for 循环,为什么不使用 auto 作为迭代器类型。为什么不使用cbegin()cend()
    • @rioV8 如果这与 Vscode 中的失败有关,请务必添加答案。
    【解决方案3】:

    您是否彻底阅读了这个官方教程? Configure VS Code for clang on macOS

    很可能您没有正确配置您的tasks.json。您的构建系统没有使用正确的 c++ 标准。 您能否发布您的tasks.json,以便我们准确了解您的配置。

    这是来自文档的tasks.json 文件配置:

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
        {
          "type": "shell",
          "label": "clang++ build active file",
          "command": "/usr/bin/clang++",
          "args": [
            "-std=c++17",
            "-stdlib=libc++",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "options": {
            "cwd": "${workspaceFolder}"
          },
          "problemMatcher": ["$gcc"],
          "group": {
            "kind": "build",
            "isDefault": true
          }
        }
      ]
    }
    

    注意:我没有评论问题的声誉,所以在这里问你。

    【讨论】:

      【解决方案4】:

      下面一行:

      vector <int> v = {1, 2, 3, 4};
      

      不会编译 C++11 之前的版本。

      但是,您可以这样做:

      static const int arr[] = {1, 2, 3, 4};
      vector<int> v ((arr, arr + sizeof(arr) / sizeof(arr[0]) );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-29
        • 1970-01-01
        • 2020-12-27
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多