【问题标题】:vector<int> cannot be initialized with an initializer list in Visual Studio Codevector<int> 无法在 Visual Studio Code 中使用初始化列表进行初始化
【发布时间】:2021-05-17 08:03:19
【问题描述】:

我在 Mac OS X 上使用 Visual Studio Code 来构建和运行一个非常基本的矢量程序。这是 代码

#include <iostream>

#include <vector>

using namespace std;

int main(){

    // Demo vector
    vector<int> arr = { 1,2,3,4,55};

    cout<<arr.size()<<endl;
    return 0;
}

下面的代码运行时出现以下错误

vectors.cpp:8:17: 错误:非聚合类型 'vector' 不能 用初始化列表初始化 向量 arr = { 1,2,3,4,55}; ^ ~~~~~~~~~~~~~ 产生1个错误。

我还在tasks.json中添加了"-std=c++11",,重启和visual studio代码,但错误依旧。这是tasks.json供参考

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "problemMatcher": [
                "$tsc"
            ],
            "group": "build",
            "label": "tsc: build - tsconfig.json"
        },
        {
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "option": "watch",
            "problemMatcher": [
                "$tsc-watch"
            ],
            "group": "build",
            "label": "tsc: watch - tsconfig.json"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "-std=c++11",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /usr/bin/clang"
        }
    ]
}

这是由 Visual Studio Code 构建的命令

cd "/Users/XXX/PROJECTS/Algorithms/" && g++ 向量.cpp -o 向量 && "/Users/XXX/PROJECTS/Algorithms/"向量

有人可以建议一种在 Visual Studio Code 编辑器中运行此程序的方法吗?

谢谢!

【问题讨论】:

  • 它可能没有链接到标准库。试试标志 -stdlib=libc++
  • 从图片中,我可以看到一些被划掉的线。而且我认为它仍然在您的任务文件中使用 g++ 而不是 clang++。你能确认是不是这样吗?
  • 这是命令 -- cd "/Users/XXX/PROJECTS/Algorithms/" && g++ vectors.cpp -o vectors && > "/Users/XXX/PROJECTS/Algorithms/"vectors跨度>
  • OT:请注意,您试图隐藏的路径在链接图像的顶部清晰可见。更多关于这个话题,你是如何开始编译过程的,使用类似代码运行器的扩展?
  • 原因确实是你没有使用预期的命令。命令行应该包含-std=c++11,但它没有。看起来 VSCode 不使用你的 json 文件...

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


【解决方案1】:

我仍然无法使用 Visual Studio 代码运行按钮来运行程序,所以我最终使用以下命令通过命令提示符运行

$ g++ -std=c++11 -o test test.cpp

如果有人可以分享他们的 task.json,那就太好了。这是我的

{
"version": "2.0.0",
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g++",
            "--std=c++11",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build",
        "detail": "compiler: /usr/bin/g++"
    }
]

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多