【问题标题】:VSCode - There is a red underline but the program builds and runs correctly, and dubugging errorVSCode - 有红色下划线,但程序构建和运行正确,调试错误
【发布时间】:2020-06-06 01:49:04
【问题描述】:

我已将我的 vscode 配置为在 Linux 上使用 c++。最近想写一个新的cpp程序,跟着MS VScode的教程,但是我把代码复制到VScode的时候,有一个红色的下划线和这样的提示: the error

但是,我可以构建它,甚至可以正确运行它而不会出错:run build tasksrun it in terminal

当我调试它时,会发生错误。

{
    "resource": "/home/aqachun/Documents/Projects/cpp/vscodeTest/helloworld.cpp",
    "owner": "C/C++",
    "severity": 8,
    "message": "no instance of constructor \"std::vector<_Tp, _Alloc>::vector [with _Tp=std::string, _Alloc=std::allocator<std::string>]\" matches the argument list -- argument types are: (const char [6], const char [4], const char [6], const char [5], const char [8], const char [23])",
    "startLineNumber": 9,
    "startColumn": 24,
    "endLineNumber": 9,
    "endColumn": 24
}

这是我的 c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "includePath": [
                "${default}",
                "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0",
                "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-pc-linux-gnu",
                "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward",
                "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include",
                "/usr/local/include",
                "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include-fixed",
                "/usr/include"
            ],
            "cppStandard": "c++17",
            "cStandard": "c11"
        }
    ],
    "version": 4
}

和我的代码:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

   for (const string& word : msg)
   {
      cout << word << " ";
   }
   cout << endl;
}

【问题讨论】:

  • 看起来像一个智能感知错误。
  • 该错误实际上是构建失败,因此您声称可以构建肯定是一种误解。至少我是这么认为的。如果你描述了你所做的和你观察到的,人们可以给出更好的诊断。作为新用户,也可以使用tour 并阅读How to Ask
  • @UlrichEckhardt 谢谢,我已经用两张关于运行构建任务和运行程序的图像更新了我的问题,它似乎运行良好吗?
  • 有趣,这对我来说用 clang++-9 编译得很好
  • 我在 vscode 中也没有出现这样的错误

标签: c++ visual-studio-code


【解决方案1】:

我也做了那个教程(对于 Linux)并且没有这样的错误。 这是我的 c_cpp_properties.json 文件,我想可能是因为您没有指定 IntelliSense。试试我的 c_cpp_properties.json:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/gcc",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

}

这是我的 settings.json:

{
"files.associations": {
    "array": "cpp",
    "atomic": "cpp",
    "*.tcc": "cpp",
    "cctype": "cpp",
    "clocale": "cpp",
    "cmath": "cpp",
    "cstdarg": "cpp",
    "cstddef": "cpp",
    "cstdint": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "cwchar": "cpp",
    "cwctype": "cpp",
    "deque": "cpp",
    "unordered_map": "cpp",
    "vector": "cpp",
    "exception": "cpp",
    "algorithm": "cpp",
    "memory": "cpp",
    "memory_resource": "cpp",
    "optional": "cpp",
    "string": "cpp",
    "string_view": "cpp",
    "system_error": "cpp",
    "tuple": "cpp",
    "type_traits": "cpp",
    "utility": "cpp",
    "fstream": "cpp",
    "initializer_list": "cpp",
    "iosfwd": "cpp",
    "iostream": "cpp",
    "istream": "cpp",
    "limits": "cpp",
    "new": "cpp",
    "ostream": "cpp",
    "sstream": "cpp",
    "stdexcept": "cpp",
    "streambuf": "cpp",
    "typeinfo": "cpp"
}

}

我的launch.json:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "g++ build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "g++ build active file",
        "miDebuggerPath": "/usr/bin/gdb"
    }
]

}

也许更改这些设置文件会有所帮助。我的工作

【讨论】:

  • 谢谢,我尝试将"intelliSenseMode" 设置为"clang-x64",但错误仍然存​​在。然后我将我的"includePath" 更改为"${workspaceFolder}/**",我得到一个#include errors detected. Please update your includePath. ,当单击灯泡添加它说要添加的includePath 时,又是原来的错误。也许我应该配置我的 c/cpp 扩展?
  • 检查你是否有clang(你可以很容易地在Linux上安装它)只要去whereis clang如果你已经安装了它会出现一个路径如果没有去sudo apt-get install clang。您可以使用 clang,它是一个正常的、良好的编译器。另外,请确保您安装了那些启用 IntelliSense 的扩展
猜你喜欢
  • 2020-03-24
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 2012-12-03
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2022-07-10
相关资源
最近更新 更多