cwcv2009
cwcv2009

Reputation: 11

Unable to initialize vector with initializer list in visual studio code

#include <vector>
#include <iostream>
using namespace std;

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

When I run the above code in vscode I receive the following ERROR:

non-aggregate type 'vector' cannot be initialized with an initializer list gcc [7, 17]

NOTICE - the error includes gcc even though that is not the compiler I am using.

The code compiles fine in the terminal and in Xcode so I know it has something to do with vscode. How do I fix this issue?

NOTE - I am using I C/C++ IntelliSense with the following configurations: Compiler Path (/usr/bin/clang++) IntelliSense mode (macros-clang-arm64) Include path (${workspaceFolder}/**) C standard (c17) C++ standard (C++17).

Upvotes: 0

Views: 3092

Answers (4)

Nisky
Nisky

Reputation: 1

On my mac, I have tried to change the CppStandard to no avail.

Instead what worked for me:

g++ -std=c++11 filename.cpp

./a.out

Upvotes: 0

Joxixi
Joxixi

Reputation: 721

I copied your code and named it test.cpp. I faced the same issue and I solved it by adding some configurations. Find tasks.json and add something in args.

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

It works on my MAC! I used command+shift+B to compile and it generated a.out after compiling. Then you can run it by F5. I also post my launch.json here, where /Users/work/Foo is my workspaceFolder. Pay attention to the line of program, I have changed this line. Good luck!

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

Upvotes: 3

Nilesh Das
Nilesh Das

Reputation: 9

Have you gone through this official tutorial thoroughly? Configure VS Code for clang on macOS

Most probably you have have not configured your tasks.json correctly. Your build system is not using the right c++ standard. Can you post your tasks.json so that we can understand your configuration exactly.

Here is the tasks.json file config from documentation:

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

Note: I don't have reputation to comment on question, so asking you here.

Upvotes: -1

Alex Reynolds
Alex Reynolds

Reputation: 96937

You might post your code and how you're compiling it. The following worked for me:

#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;
}

Compiled and run like so:

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

Upvotes: 2

Related Questions