0
#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).

cwcv2009
  • 11
  • 4
  • 3
    Did you enable c++11 or later? – ChrisMM Jul 09 '21 at 23:50
  • How would I check whether I am using C++11 or later in VSCode? I have been search the web for a clear and concise answer but to no avail. – cwcv2009 Jul 10 '21 at 02:11
  • it might be result of default settings being baked into your project already, Are you using platformio? You might need upgrade it or fix its config file. – Swift - Friday Pie Aug 03 '21 at 08:32
  • Your code works fine in the latest gcc: https://gcc.godbolt.org/z/nqcvsecd6 – Fedor Aug 04 '21 at 15:18
  • I know the code works fine on online compilers. It also works just fine in my terminal. This issue is somewhere between MacOS and VSCode. – cwcv2009 Aug 05 '21 at 16:07

4 Answers4

3

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
    }
  ]
}
Joxixi
  • 651
  • 5
  • 18
2

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
$
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • I copy and pasted your code and saved it to a test.cpp file, then ran the same two commands in the terminal: $ g++ -std=c++11 test.cpp $ ./a.out Then received the same output: 1 2 3 4 However, when I run test.cpp in vscode I receive the following error: "non-aggregate type 'std::vector' cannot be initialized with an initializer list" Why is this? My configs are as followed: Compiler Path = /usr/bin/g++ IntelliSense Mode = macos-gcc-x64 C++ standard = c++17 – cwcv2009 Jul 10 '21 at 18:16
  • I honestly don't know much about vscode or how to configure it to compile C++ code. Maybe this answer would help with setting flags: https://stackoverflow.com/a/57173879/19410 I wish I could help. You might edit your question with these details and I'll vote to reopen. Or ask a new question? – Alex Reynolds Jul 10 '21 at 19:12
  • 1
    I edited the question. A vote to reopen the question would help greatly. I cannot ask another question due to I asking the question incorrectly the first two times. – cwcv2009 Jul 11 '21 at 02:26
  • why write the for loop full if we have ranged for loops, and why not use `auto` for the iterator type. why not use `cbegin()` and `cend()` – rioV8 Aug 01 '21 at 00:45
  • @rioV8 If this is relevant to what is failing in Vscode, definitely add an answer. – Alex Reynolds Aug 01 '21 at 23:18
0

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

Nisky
  • 1
  • 2
  • Hello Nisky, I wanted to clarify that the question is specifically about how to specify the C++ standard in VSCode, rather than in the terminal. – Alireza Roshanzamir Jul 16 '23 at 18:03
-1

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.

Nilesh Das
  • 9
  • 1
  • 3