1

I am unable to initialize a vector in C++ macOS, VSCode. I looked at possible solutions, but i did not find any solution. I'm stuck at this point

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> v{3, 5, 5, 1};
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
} 

This is my error:

text2.cpp:9:18: error: expected ';' at end of declaration
    vector<int> v{3, 5, 5, 1};
                 ^
                 ;
1 error generated.

This is my tasks.json file.

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fdiagnostics-color=always",
                "-std=c++17",
                "-stdlib=libc++",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}",
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
  • 2
    About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – and much worse [why you shouldn't ever include bits/stdc++.h](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). The latter likely is the cause of the error as well as the header is GCC specific but default compiler for macOS is clang. – Aconcagua Aug 20 '22 at 10:04
  • Compiles for me - live - https://godbolt.org/z/EjE3oGTse – Richard Critten Aug 20 '22 at 10:08
  • 1
    The error message does not match the source. – n. m. could be an AI Aug 20 '22 at 10:42
  • 2
    @Aconcagua, I have removed "bits/stdc++.h" and changed it to "vector" and "iostream". I removed "using namespace std" and now use "std :: " but it still doesn't help. – Utkarsh Gupta Aug 20 '22 at 10:42
  • The error hints at the code being compiled as C++98 (which didn't have this kind of initialization). I don't know enough VSCode to tell *what* is wrong in the config, but something is. – BoP Aug 20 '22 at 13:03
  • Thank you all for the suggestions, I learnt a lot of new things. In the end i installed gcc from home brew and now am using that. – Utkarsh Gupta Aug 20 '22 at 13:04
  • 1
    How to initialize a `std::vector` is *not* OS specific. You using MacOS is irrelevant. – Jesper Juhl Oct 23 '22 at 00:22
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jesper Juhl Oct 23 '22 at 00:22
  • This is a vscode problem. You've to correctly configure your vscode to run C++11 or higher. In particular make sure your *c_cpp_properties.json* file has `"cStandard": "gnu17", "cppStandard": "gnu++17",` – Jason Nov 12 '22 at 06:41

1 Answers1

-3

The easiest solution is to use parenthesis instead of curly braces:

E.g.: int a (0);

James Risner
  • 5,451
  • 11
  • 25
  • 47