Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix namespace "std" has no member "sqrt" in VSCode?

I am able to compile and execute my code successfully. However, VSCode keeps showing me the error message:

namespace std has no member "sqrt".

I adjusted the properties.json. Please advice why is the vscode showing this error. I tried googling but no avail.

#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
}
like image 437
hellopanda Avatar asked Nov 06 '22 18:11

hellopanda


1 Answers

I see two problems with your c_cpp_properties.json:

  1. There is no compilerPath attribute.
  2. You have intelliSenseMode as msvc-x64 but are clearly using gcc include paths.

Probably you want to fix (1) by providing the full path to g++.exe and (2) by changing intelliSenseMode to gcc-x86. Something like:

{
    "configurations": [
        {
            ...
            "compilerPath": "C:/MinGW/bin/g++.exe",
            "intelliSenseMode": "gcc-x86",
            ...
        }
    ],
    "version": 4
}

I also suggest going through the Getting Started with C++ guide if you haven't already. Even if you don't ultimately want to set things up the way the tutorial does, it is valuable to have a working configuration to compare to when things go wrong.

Also, in the Command Palette (Ctrl+Shift+P), try running "C/C++: Log Diagnostics". Compare what you see in that output to the output of:

  $ touch empty.c
  $ g++ -v -E -dD empty.c

Ideally, you want those to match as closely as possible.

like image 160
Scott McPeak Avatar answered Nov 14 '22 23:11

Scott McPeak