如何修复 VSCode 中命名空间“std”没有成员“sqrt”?

hel*_*nda 5 c++ visual-studio-code

我能够成功编译并执行我的代码。但是,VSCode 不断向我显示错误消息:

命名空间 std 没有成员“sqrt”。

我调整了properties.json。请告知为什么 vscode 显示此错误。我尝试用谷歌搜索但没有成功。

#include <iostream>
#include <cmath>
#include <complex>

int main() {
 double a,b,c;
 int root1, root2;


  std::cout<<"Enter a: \n";
  std::cin >> a;
  std::cout<<"Enter b: \n";
  std::cin >> b;
  std::cout<<"Enter c: \n";
  std::cin >> c;

  root1 = (-b + std::sqrt (b*b - 4*a*c)) / (2*a);
  std::cout<<"Root 1 Number: " << root1 << "\n";

  root2 = (-b - std::sqrt (b*b - 4*a*c)) / (2*a);
  std::cout<<"Root 2 Number: " << root2 << "\n";

}

json:
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/MinGW/lib/gcc/mingw32/8.2.0/include/c++",
                "C:/MinGW/lib/gcc/mingw32/8.2.0/include/c++/backward",
                "C:/MinGW/lib/gcc/mingw32/8.2.0/include",
                "C:/MinGW/lib/gcc/mingw32/8.2.0/include/c++/tr1",
                "C:/MinGW/lib/gcc/mingw32/8.2.0/include/c++/tr2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "C:/MinGW/lib/gcc/mingw32/8.2.0/include/c++",
                    "C:/MinGW/lib/gcc/mingw32/8.2.0/include/c++/backward",
                    "C:/MinGW/lib/gcc/mingw32/8.2.0/include",
                    "C:/MinGW/lib/gcc/mingw32/8.2.0/include/c++/tr1",
                    "C:/MinGW/lib/gcc/mingw32/8.2.0/include"
                ]
            }

        }
    ],
    "version": 4
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*eak 2

我发现你的有两个问题c_cpp_properties.json

  1. 没有compilerPath属性。
  2. 您有intelliSenseModeasmsvc-x64但显然正在使用gcc包含路径。

您可能希望通过提供完整路径来修复 (1) g++.exe,并通过更改intelliSenseMode为 来修复 (2) gcc-x86。就像是:

{
    "configurations": [
        {
            ...
            "compilerPath": "C:/MinGW/bin/g++.exe",
            "intelliSenseMode": "gcc-x86",
            ...
        }
    ],
    "version": 4
}
Run Code Online (Sandbox Code Playgroud)

如果您还没有阅读C++ 入门指南,我还建议您阅读一下。即使您最终不想按照教程的方式进行设置,拥有一个工作配置来与出现问题时进行比较也是很有价值的。

另外,在命令面板 (Ctrl+Shift+P) 中,尝试运行“C/C++:日志诊断”。将您在该输出中看到的内容与以下内容的输出进行比较:

{
    "configurations": [
        {
            ...
            "compilerPath": "C:/MinGW/bin/g++.exe",
            "intelliSenseMode": "gcc-x86",
            ...
        }
    ],
    "version": 4
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,您希望它们尽可能匹配。