I'm very new to C++, working through my first tutorial, and when I try to compile code from the lesson, I get the following error:
expected ';' at end of declaration
int x{ }; // define variable x to hold user input (a...
^
;
The full code for the program I'm attempting to run:
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: ";
int x{ };
std::cin >> x;
std::cout << "You entered " << x << '\n';
return 0;
}
I am using Visual Studio Code (v.1.46.1) on a Macbook Pro, with the Microsoft C/C++ extension (https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools).
My compiler is Clang:
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Initially, I ran Terminal > Configure Default Build Task in VS Code to create a .vscode/tasks.json compiler settings file. That file currently looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
// Set C++ Standards
"-std=c++17",
// Increase compiler warnings to maximum
"-Wall",
"-Weffc++",
"-Wextra",
"-Wsign-conversion",
// Treat all warnings as errors
"-Werror",
// Disable compiler extensions
"-pedantic-errors",
// File to compile
"-g",
"${file}",
// Output file
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I have the -std=c++17
flag set, which should allow direct brace initialization from what I understand.
I'm not sure it matters, since I'm trying to compile and not build/debug, but for the sake of thoroughness, I also have a .vscode/launch.json file with the following contents:
{
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
Can someone help me figure out why int x{ };
is not working properly to intitialize the variable and what I can do to fix it so it will work?
[Edit]: Further settings I've checked/tested:
- Code compiles correctly when running compile directly from command line with
clang++ -std=c++17 -g helloworld.cpp -o helloworld
- VS Code C/C++ extension configuration has setting 'C++ standard' set to c++17 (seems to be the default). Even so, running command-line compile without
-std=c++17
flag set causes same compiler error. - Tried changing
int x{ };
to the following:int x( );
: fails with a very long list of errorsint x(0);
: compiles successfullyint x = { };
: compiles successfullyint x = {0};
: compiles successfully- `int x;': compiles successfully
- `int x = 0;': compiles successfully
helloworld.cpp
. This is the only error I get. Full console output is: ``` Executing task: /usr/bin/clang++ -g /Users/{redacted}/Documents/development/c++/learncpp/helloworld/helloworld.cpp -o /Users/{redacted}/Documents/development/c++/learncpp/helloworld/helloworld < /Users/{redacted}/Documents/development/c++/learncpp/helloworld/helloworld.cpp:6:10: error: expected ';' at end of declaration int x{ }; ^ ; 1 error generated. The terminal process terminated with exit code: 1 ``` – Proclaim-std=c++17
is obviously not being passed, and your compiler is old enough to default to C++03, where that syntax doesn’t work. – Onionsint x = {0};
– Caesuraint x={};
? Anyway the compiler is based on llvm9 which is not old enough to default to c++03. – Caesura=
with a single expression in braces. The C++11 extensions included empty braces, and omitting the=
symbol – Whitneywhitson