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 ofstd
I have already included math.h
.
What is the problem with that?
'sqrt' is not a member of 'std'
The sqrt() function in C++ returns the square root of a number. This function is defined in the cmath header file.
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.
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 correspondingcname
header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope of the namespacestd
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With