问题
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
回答1:
Refering to this explanation of List initialization, this syntax should be legal since c++11:
int main()
{
int n0{}; // value-initialization (to zero)
int n1{1}; // direct-list-initialization
//...
}
I attempted to compile your code in my local environment and all worked fine:
clang++ -std=c++17 -Wall -Weffc++ -Wextra -Wsign-conversion -Werror -pedantic-errors ...
clang++ --version
clang version 9.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
Maybe you used some similar ';' character which is not ASCII?
I attempted to use a Chinese character ';' instead and get this:
fly.cc:32:13: error: treating Unicode character <U+FF1B> as identifier character rather than as ';' symbol [-Werror,-Wunicode-homoglyph]
int x{ };
^~
fly.cc:32:13: error: expected ';' at end of declaration
int x{ };
^
;
来源:https://stackoverflow.com/questions/62487546/c-compile-error-expected-at-end-of-declaration-when-using-direct-brace-i