VS代码c++;对于macOS:编译工作正常,但VS代码检测到错误 我遵循了本网页的教程:能够用VS代码编译C++程序。在完成所有需要的操作之后,我可以编译和调试C++文件。

VS代码c++;对于macOS:编译工作正常,但VS代码检测到错误 我遵循了本网页的教程:能够用VS代码编译C++程序。在完成所有需要的操作之后,我可以编译和调试C++文件。,c++,visual-studio-code,c++17,C++,Visual Studio Code,C++17,然而,似乎有一个问题。在“问题”框中,我有以下两个错误: 期望“;”在声明末尾[9,23] 基于范围的for循环是一个C++11扩展[-Wc++11扩展][11, 29] 我的代码与VS代码网站上报告的代码完全相同。我还检查了所有*.json文件,c++17是默认编译器 tasks.json如下所示: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.j

然而,似乎有一个问题。在“问题”框中,我有以下两个错误:

期望“;”在声明末尾[9,23]

基于范围的for循环是一个C++11扩展[-Wc++11扩展][11, 29]

我的代码与VS代码网站上报告的代码完全相同。我还检查了所有*.json文件,c++17是默认编译器

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
    }
  }
]
{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [],
        "macFrameworkPath": [
            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4
}

c_cpp_properties.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
    }
  }
]
{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [],
        "macFrameworkPath": [
            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4
}

编译时,一切正常:

> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/stephane/Documents/c++/causality/run_mainfile.cpp -o /Users/stephane/Documents/c++/causality/run_mainfile <
Terminal will be reused by tasks, press any key to close it.
>执行任务:/usr/bin/clang++-std=c++17-stdlib=libc++-g/Users/stephane/Documents/c++/causality/run_mainfile.cpp-o/Users/stephane/Documents/c++/causality/run_mainfile<
终端将被任务重用,请按任意键将其关闭。
以下是截图:

我使用的是macOS 10.15.6、VS代码1.47.1和C/C++扩展v0.29


有什么想法吗?

听起来你并不是在C++17模式下运行编译器。在问题中显示完整的构建设置和生成的命令行。您是否安装了Intellisens或类似的插件,在键入时添加自动完成和语法检查?可能是这个工具使用不同的设置,然后编译器会报告不同的错误。我为VS代码安装了C++扩展,包括智能感知。我现在还报告了一些*.json文件。@rangerob我觉得您的配置文件很好。尝试重新启动VSCode,看看问题是否仍然存在。@RangerBob现在没有任何正确的消息。配置似乎一切正常。您可以尝试一件事:创建一个新目录。使用vscode打开它,然后打开首选项>搜索>“c++标准”。选择C++17或C++20,以可用的为准。别做别的。创建文件并检查问题是否仍然存在。我知道这不是一个“解决方案”,虽然你可以尝试一下,但不会造成太大的伤害。如果问题仍然存在,请在此处提出问题:。