使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"

c++ compile error 'expected ';' at end of declaration' when using direct brace initialization

本文关键字:声明 错误 编译 C++ 初始化      更新时间:2023-10-16

我对C++非常陌生,正在学习我的第一个教程,当我试图编译课程中的代码时,我得到了以下错误:

expected ';' at end of declaration
int x{ }; // define variable x to hold user input (a...
^
;

我试图运行的程序的完整代码:

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

我在Macbook Pro上使用Visual Studio Code(1.46.1版),带有Microsoft C/C++扩展(https://marketplace.visualstudio.com/items?itemName=ms-vscode.cptools)。

我的编译器是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

最初,我运行了Terminal>在VS代码中配置默认构建任务以创建.vscode/tasks.json编译器设置文件。该文件当前如下所示:

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

我已经设置了-std=c++17标志,根据我的理解,它应该允许直接初始化大括号。

我不确定这是否重要,因为我试图编译而不是构建/调试,但为了彻底起见,我还有一个.vscode/launch.json文件,其中包含以下内容:

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

有人能帮我弄清楚int x{ };为什么不能正确初始化变量,以及我能做些什么来修复它,使其正常工作吗?

[编辑]:我检查/测试过的其他设置:

  • 使用clang++ -std=c++17 -g helloworld.cpp -o helloworld直接从命令行运行compile时,代码会正确编译
  • VS代码C/C++扩展配置的设置"C++标准"设置为C++17(似乎是默认设置)。即便如此,在不设置-std=c++17标志的情况下运行命令行编译也会导致相同的编译器错误
  • 已尝试将int x{ };更改为以下内容:
    • int x( );:失败,错误列表很长
    • int x(0);:编译成功
    • int x = { };:编译成功
    • int x = {0};:编译成功
    • `int x;':编译成功
    • `int x=0;':编译成功

我遇到了同样的问题,在tasks.json中出现了错误。这对我很有效,试试看:

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

我遇到了完全相同的问题,我试图修改tasks.json、c_cpp_properties.json和settings.json,但没有成功。我终于尝试了这里显示的解决方案,即禁用C/C++Clang命令适配器扩展,它对我很有效!

参考列表初始化的解释,由于c++11:,此语法应该是合法的

int main()
{
int n0{};     // value-initialization (to zero)
int n1{1};    // direct-list-initialization
//...
}

我试图在我的本地环境中编译你的代码,一切都很好:

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

也许你用了一些类似的";"不是ASCII的字符
我试图使用汉字";"相反,得到这个:

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{ }; 
^
;

我也被这个问题困扰了一段时间。这是让它为我工作的命令:

clang++ helloworld.cpp -o helloworld -std=c++17 && "<YOUR PATH TO THE FILE INSIDE THE QUOTES>"helloworld

例如"< YOUR PATH TO THE FILE INSIDE THE QUOTES >" = "/Users/< your username >/<something else>/"helloworld

我还安装了扩展code runner,然后在设置中搜索run in terminal,然后选中了说Code-runner: Run in terminal的框。

您也应该能够在设置中搜索C++,然后设置默认的Cpp标准,但这对我来说从来都不起作用,所以我不得不在命令中添加它。