C++ VS Code 无法识别语法,无法运行代码

mpp*_*mpp 0 c++ macos visual-studio-code c++17

我正在使用课程所需的特定语法,但是当我在 VS Code 中使用此 C++ 语法时,它不起作用并会引发错误。

以下是无效语法的示例:

error: expected ';' at end of declaration
        int i {0}; 
             ^
             ;
Run Code Online (Sandbox Code Playgroud)

当我将其更改为int i = 0;错误时,错误就消失了。

它特别不识别{}设置默认变量值的语法。我ssh在本课程中使用登录名,语法在 中运行良好ssh,但在 VS Code 中不起作用。

我尝试通过执行此线程中的最佳答案将 VS Code C++ 版本更改为 C++17 ,但它仍然无法识别语法。

我是否使用了不正确的语法,或者有办法解决这个问题吗?

mpp*_*mpp 5

添加上面的评论,这就是解决我的问题的方法:

转到此链接: https://code.visualstudio.com/docs/cpp/config-clang-mac

转到macOS 上的 Clang部分,然后向下滚动到故障排除。请按照本段中的步骤操作:

“如果您看到提及“C++11 扩展”的构建错误,则可能没有更新您的tasks.json 构建任务以使用 clang++ 参数 -- std=c++17。默认情况下,clang++ 使用 C++98 标准,该标准不支持 C++98 标准。支持 中使用的初始化。请确保使用Run helloworld.cpphelloworld.cpp部分中提供的代码块替换tasks.json 文件的全部内容。”

您需要复制粘贴此确切代码并替换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": "C/C++: 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
      },
      "detail": "Task generated by Debugger."
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

要找到该tasks.json文件夹​​,请转到您的程序文件,执行Command+ Shift+.查找隐藏文件>>>.vscodetasks.json文件中的所有代码替换为我提供的代码。它更新了 C++ 编译器以使用可识别语法的更新版本的 C++。