c ++编译错误'预期';' 在声明结束时使用直接大括号初始化

M. *_*ton 7 c++ compiler-errors visual-studio-code c++17

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

expected ';' at end of declaration
    int x{ }; // define variable x to hold user input (a...
         ^
         ;
Run Code Online (Sandbox Code Playgroud)

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

我的编译器是 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
Run Code Online (Sandbox Code Playgroud)

最初,我在 VS Code 中运行终端 > 配置默认构建任务来创建一个 .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
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

-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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 直接从命令行运行编译时,代码编译正确 clang++ -std=c++17 -g helloworld.cpp -o helloworld
  • VS Code 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;':编译成功

小智 7

我遇到了同样的问题,并且我的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
        }
      }
    ]
  }
Run Code Online (Sandbox Code Playgroud)


rus*_*yhu 0

参考List初始化的这个解释, this syntax should be legal since c++11:

\n
int main()\n{\n    int n0{};     // value-initialization (to zero)\n    int n1{1};    // direct-list-initialization\n    //...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试在本地环境中编译您的代码,一切正常:

\n
clang++ -std=c++17 -Wall -Weffc++ -Wextra -Wsign-conversion -Werror -pedantic-errors ...\n\nclang++ --version\nclang version 9.0.1 \nTarget: x86_64-pc-linux-gnu\nThread model: posix\n
Run Code Online (Sandbox Code Playgroud)\n

也许您使用了一些类似的非 ASCII 字符 \';\' ?
\n我尝试使用汉字 \'\xef\xbc\x9b\' 代替并得到:

\n
fly.cc:32:13: error: treating Unicode character <U+FF1B> as identifier character rather than as \';\' symbol [-Werror,-Wunicode-homoglyph]\n    int x{ }\xef\xbc\x9b \n            ^~\nfly.cc:32:13: error: expected \';\' at end of declaration\n    int x{ }\xef\xbc\x9b \n            ^\n            ;\n
Run Code Online (Sandbox Code Playgroud)\n

  • 这实际上不是一个汉字,只是一个[全角](https://unicodeplus.com/block/FF00)分号...... (2认同)