26

I compile my program in Linux - it has the following line:

std::sqrt((double)num);

On Windows, it is ok. However, on Linux, I get an error:

sqrt is not a member of std

I have already included math.h.

What is the problem with that?

Milan
  • 1,743
  • 2
  • 13
  • 36
YAKOVM
  • 9,805
  • 31
  • 116
  • 217

2 Answers2

36

Change the directive to #include <cmath>. C++ headers of the form <cxxxxxxx> are guaranteed to have the standard names in std namespace (and may optionaly provide them in global namespace). <xxxxxx.h> are not.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • 1
    `guaranteed to have the standard names in std namespace` before `c++11`. Of course the reason that was relaxed for 11 was that some implementations never bothered. – BoBTFish May 13 '13 at 09:04
  • 5
    @BoBTFish It is still guaranteed to have the standard names in `std` namespace. Pre C++11, it was also guaranteed _not_ to have them in the global namespace. C++11 allows them to also be in global namespace, since this was the most widespread existing practice. – James Kanze May 13 '13 at 09:06
  • 1
    @BoBTFish What JamesKanze said, see 17.6.1.2/4. – jrok May 13 '13 at 09:10
  • 1
    Yup, sorry about that, I posted before I went looking for confirmation. I won't delete it, that'd just make the subsequent comments confusing. – BoBTFish May 13 '13 at 09:18
6

it is simply because <math.h> does not declare the functions in namespace std. It has been included into the C++ standard only for compatibility reasons. The correct C++ include would be <cmath>.

§D.5,2

Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope of the namespace std and are then injected into the global namespace scope by explicit using-declarations.

That your code worked under windows was pure luck - if you want to call it so. The last sentence gives a hint what might happen under windows, but not under linux: under windows, obviously the names are valid in both the global namespace and namespace std.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • `` is part of standard C, as a "compatibilty feature". There are, in fact, good reasons to prefer it, at least in certain cases. Of course, it declares `::sqrt`, and not `std::sqrt`. – James Kanze May 13 '13 at 09:04
  • @JamesKanze thanks for the correction, I wasn't aware of that. I changed my answer. – Arne Mertz May 13 '13 at 09:20