C ++编译错误'expected';'在声明的末尾'使用直接括号初始化时 [英] c++ compile error 'expected ';' at end of declaration' when using direct brace initialization

查看:565
本文介绍了C ++编译错误'expected';'在声明的末尾'使用直接括号初始化时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,正在阅读第一个教程,当我尝试从本课编译代码时,出现以下错误:

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我正在尝试运行的程序的完整代码:

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

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

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).

我的编译器是Clang:

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

最初,我运行Terminal>在VS Code中配置默认​​构建任务,以创建.vscode / tasks.json编译器设置文件。该文件当前看起来像这样:

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

我有 -std = c ++ 17 标志设置,应该允许根据我的理解直接进行括号初始化。

I have the -std=c++17 flag set, which should allow direct brace initialization from what I understand.

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

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

有人可以帮我弄清楚为什么 int x {}; 无法正常使用该变量的初始化,而我可以如何解决该问题以使其起作用呢?

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?

:我已经检查/测试的其他设置:

: Further settings I've checked/tested:


  • 直接使用 clang ++ -std = c从命令行运行编译时,代码可以正确编译++ 17 -g helloworld.cpp -o helloworld

  • VS代码C / C ++扩展配置将'C ++ standard'设置为c ++ 17(似乎为默认值)。即便如此,在未设置 -std = c ++ 17 标志的情况下运行命令行编译也会导致相同的编译器错误。

  • 尝试更改 int x {}; 到以下内容:

    • int x(); :失败,并带有很长的错误列表

    • int x(0); :编译成功

    • int x = {}; :成功编译

    • int x = {0 }; :编译成功

    • `int x;':编译成功

    • `int x = 0;':成功编译

    • 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 errors
      • int x(0);: compiles successfully
      • int x = { };: compiles successfully
      • int x = {0};: compiles successfully
      • `int x;': compiles successfully
      • `int x = 0;': compiles successfully

      推荐答案

      请参考对列表初始化的说明,由于c ++ 11:

      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
      

      也许您使用了类似的';'字符而不是ASCII?

      我试图使用中文字符;代替:

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

      这篇关于C ++编译错误'expected';'在声明的末尾'使用直接括号初始化时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆