8

我试图从工作区运行一个 .cpp 文件,但给了我这个关于不添加 c++11/higher 标志的错误,但我已将它们添加到 task.json

错误

[Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main
main.cpp:8:1: error: expected unqualified-id before 'using'
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
^
main.cpp:10:1: error: expected unqualified-id before 'using'
using ordered_set_rev = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
^
main.cpp:12:1: error: expected unqualified-id before 'using'
using dijkstra = priority_queue<T, vector<T>, greater<T>>;
^
main.cpp:62:31: warning: variadic templates only available with -std=c++11 or -std=gnu++11
template <typename T, typename... Args>
                               ^
main.cpp:63:52: warning: variadic templates only available with -std=c++11 or -std=gnu++11
void err(istream_iterator<string> it, T a, Args... args) {


任务.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-o",
                "test",
                "-std=c++14",
                "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

错误信息:

[Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main
main.cpp:8:1: error: expected unqualified-id before 'using'
 using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 ^
main.cpp:10:1: error: expected unqualified-id before 'using'
 using ordered_set_rev = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
 ^
main.cpp:12:1: error: expected unqualified-id before 'using'
 using dijkstra = priority_queue<T, vector<T>, greater<T>>;
 ^
main.cpp:62:31: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename T, typename... Args>
                               ^
main.cpp:63:52: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 void err(istream_iterator<string> it, T a, Args... args) {

c_cpp_properties:

    {

        "name": "Win32",
        "includePath": [
            "${workspaceFolder}"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "intelliSenseMode": "clang-x64",
        "browse": {
            "path": [
                "${workspaceFolder}"
            ],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        },
        "compilerPath": "F:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gcc.exe",
        "cStandard": "c11",
        "cppStandard": "c++17"
    }
4

5 回答 5

19

我添加了代码运行器并将其添加到 settings.json 似乎对我有用:D

"code-runner.executorMap": {
    "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
于 2019-03-12T22:27:37.173 回答
3
  • 打开 VS 代码
  • 转到扩展程序(Ctrl + Shift + X)
  • 然后只需单击代码运行器
  • 然后点击那里的设置图标
  • 然后选择扩展设置
  • 照片
  • 设置将打开,

  • 点击 Code-Runner -Executer Map 下 settings.json 中的编辑

  • 照片

  • 在json文件中找到“cpp”,在&& g++后面加上-std=c++17
  • 最后它看起来像这样 "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  • 保存它,你就完成了]
  • 照片
  • 如果此代码运行没有任何错误,请尝试此代码您已启用 C++17 链接
  • 确保在运行代码终端时看起来像这样
  • 照片
  • 注意:如果您不使用代码 Runner,我会建议您这样做
  • 如果您使用的是 windows 并且您的编译器不支持 c++17
  • 您可以从以下链接安装 MinGw 最新编译器(9.2.0)
  • 关联
于 2021-03-03T06:09:27.503 回答
2

添加"-std=c++11",到文件中的"args"task.json。那应该可以解决您的 c++11 问题。所以最后你的 task.json 看起来像

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",                
                "-std=c++11",
                "-std=c++14",
                "main.cpp",
                "-o",
                "test"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}
于 2019-08-21T23:49:37.133 回答
1

分别设置cppStandardc++17c++14

您需要该https://github.com/Microsoft/vscode-cpptools的 C++ 扩展

于 2019-03-12T08:06:11.203 回答
0

我没有在 c_cpp_properties.json 添加 MinGW 头文件。在它工作正常之后 c_cpp_properties.json 看起来像这样......

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "C:\\Program Files (x86)\\CodeBlocks\\MinGW\\include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "\"C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\g++.exe\"",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "gcc-x86"
    }
],
"version": 4
}

并将我的 tasks.json 更改为此 ...

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "g++.exe build active file",
        "command": "g++.exe -std=c++14 -g ${file} -o ${fileDirname}\\${fileBasenameNoExtension}.exe && ${fileDirname}\\${fileBasenameNoExtension}.exe",
        "options": {
            "cwd": "C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build"
    }
]
}

现在它构建并运行编译的文件

于 2019-11-08T10:38:33.610 回答