使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
c++ compile error 'expected ';' at end of declaration' when using direct brace initialization
我对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标准,但这对我来说从来都不起作用,所以我不得不在命令中添加它。
- 在气泡排序程序中未声明错误功能
- 未在此范围内声明错误 'xy'
- 在此范围内未声明错误"Clreol"
- 为什么 C 编译器不在 for 循环体中给出重新声明错误?
- 如何修复范围内的字符串声明错误
- 如何修复C++中的"arrStud未在此范围内声明"错误
- 无法识别的模板声明错误 - 不明白我做错了什么
- C 如何从构造函数中声明(错误C2065未宣布的标识符)
- C 无法解释的类“尚未声明”错误,这是由于名称空间而导致的
- 如何解决声明错误
- 在标题文件中获取声明错误
- 异常声明错误
- "inet_ntoa"未声明错误
- #include <string> 导致很多'<blank>'尚未声明错误
- 类声明错误
- C++成员函数未声明错误时(当它看起来是错误时)
- 代码块 HelloWorld C 示例不起作用:未声明错误"ptrdiff_t"
- 头文件类成员函数声明错误:"incomplete type name in nested name specifier"
- 预期语句和预期声明错误 c++
- 针对 android 编译的问题..."未在此范围内声明"错误