Rohit
Rohit

Reputation: 7161

vector<int> cannot be initialized with an initializer list in Visual Studio Code

I am using Visual Studio Code on Mac OS X to build and run a very basic vector program. Here is the 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;
}

The below code on running gives following error

vectors.cpp:8:17: error: non-aggregate type 'vector' cannot be initialized with an initializer list vector arr = { 1,2,3,4,55}; ^ ~~~~~~~~~~~~~ 1 error generated.

I also added "-std=c++11", in tasks.json, restarted and visual studio code, but the error remains the same. Here is the tasks.json for reference

{
    "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"
        }
    ]
}

enter image description here

this is the command being built by Visual Studio Code

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

Can some one suggest a way to run this program within Visual Studio Code editor?

Thanks!

Upvotes: 0

Views: 724

Answers (1)

Rohit
Rohit

Reputation: 7161

I still could not use the Visual studio code run button to run the program, so I ended up running via command prompt using below command

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

If someone can share their task.json, it will be good to see. Here is mine

{
"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++"
    }
]

}

Upvotes: 1

Related Questions