math.h and cmath problem

Aug 3, 2010 at 9:35pm
I've been trying to use math.h in one of my programs, but nothing's working. I'm using VC++ 2008. This simple code fails.
1
2
3
4
5
6
7
8
#include <iostream>
#include <math.h>

int main()
{
    std::cout << sqrt(9);
    return 0;
}

This reports:

c:\documents and settings\shane\desktop\messing around\main.cpp(6) : error C3861: 'sqrt': identifier not found

This also fails:
1
2
3
4
5
6
7
8
#include <iostream>
#include <cmath>

int main()
{
    std::cout << std::sqrt(9);
    return 0;
}

It reports over one hundred errors.
Aug 3, 2010 at 9:46pm
If your compiler does not provide overloaded functions for integral types (it isn't required to), you'll have to explicitly cast to another type, namely one of these three:

http://www.cplusplus.com/reference/clibrary/cmath/sqrt/

...or just provide a double literal in the first place:
std::sqrt(9.0)
Aug 3, 2010 at 9:54pm
That's not the problem, the problem is that for some reason the sqrt() (and sin() and cos()) functions don't seem to be defined. The funny thing is, I've used sqrt() before and it worked.
Aug 3, 2010 at 10:24pm
So perhaps you should post these "over one hundred errors".
Aug 3, 2010 at 10:43pm
Mkay, here they are.
c:\program files\microsoft visual studio 9.0\vc\include\cmath(21) : error C2039: 'acosf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(21) : error C2873: 'acosf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(21) : error C2039: 'asinf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(21) : error C2873: 'asinf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(22) : error C2039: 'atanf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(22) : error C2873: 'atanf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(22) : error C2039: 'atan2f' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(22) : error C2873: 'atan2f' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(22) : error C2039: 'ceilf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(22) : error C2873: 'ceilf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(23) : error C2039: 'cosf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(23) : error C2873: 'cosf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(23) : error C2039: 'coshf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(23) : error C2873: 'coshf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(23) : error C2039: 'expf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(23) : error C2873: 'expf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(24) : error C2039: 'fabsf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(24) : error C2873: 'fabsf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(24) : error C2039: 'floorf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(24) : error C2873: 'floorf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(24) : error C2039: 'fmodf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(24) : error C2873: 'fmodf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(25) : error C2039: 'frexpf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(25) : error C2873: 'frexpf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(25) : error C2039: 'ldexpf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(25) : error C2873: 'ldexpf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(25) : error C2039: 'logf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(25) : error C2873: 'logf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(26) : error C2039: 'log10f' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(26) : error C2873: 'log10f' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(26) : error C2039: 'modff' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(26) : error C2873: 'modff' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(26) : error C2039: 'powf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(26) : error C2873: 'powf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(27) : error C2039: 'sinf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(27) : error C2873: 'sinf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(27) : error C2039: 'sinhf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(27) : error C2873: 'sinhf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(27) : error C2039: 'sqrtf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(27) : error C2873: 'sqrtf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(28) : error C2039: 'tanf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(28) : error C2873: 'tanf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(28) : error C2039: 'tanhf' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(28) : error C2873: 'tanhf' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(30) : error C2039: 'acosl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(30) : error C2873: 'acosl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(30) : error C2039: 'asinl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(30) : error C2873: 'asinl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(31) : error C2039: 'atanl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(31) : error C2873: 'atanl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(31) : error C2039: 'atan2l' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(31) : error C2873: 'atan2l' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(31) : error C2039: 'ceill' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(31) : error C2873: 'ceill' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(32) : error C2039: 'cosl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(32) : error C2873: 'cosl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(32) : error C2039: 'coshl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(32) : error C2873: 'coshl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(32) : error C2039: 'expl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(32) : error C2873: 'expl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(33) : error C2039: 'fabsl' : is not a member of '`global namespace''
Last edited on Aug 3, 2010 at 10:45pm
Aug 3, 2010 at 10:43pm
c:\program files\microsoft visual studio 9.0\vc\include\cmath(33) : error C2873: 'fabsl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(33) : error C2039: 'floorl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(33) : error C2873: 'floorl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(33) : error C2039: 'fmodl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(33) : error C2873: 'fmodl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(34) : error C2039: 'frexpl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(34) : error C2873: 'frexpl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(34) : error C2039: 'ldexpl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(34) : error C2873: 'ldexpl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(34) : error C2039: 'logl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(34) : error C2873: 'logl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(35) : error C2039: 'log10l' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(35) : error C2873: 'log10l' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(35) : error C2039: 'modfl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(35) : error C2873: 'modfl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(35) : error C2039: 'powl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(35) : error C2873: 'powl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(36) : error C2039: 'sinl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(36) : error C2873: 'sinl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(36) : error C2039: 'sinhl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(36) : error C2873: 'sinhl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(36) : error C2039: 'sqrtl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(36) : error C2873: 'sqrtl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(37) : error C2039: 'tanl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(37) : error C2873: 'tanl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(37) : error C2039: 'tanhl' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(37) : error C2873: 'tanhl' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(41) : error C2039: 'acos' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(41) : error C2873: 'acos' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(41) : error C2039: 'asin' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(41) : error C2873: 'asin' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(42) : error C2039: 'atan' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(42) : error C2873: 'atan' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(42) : error C2039: 'atan2' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(42) : error C2873: 'atan2' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(42) : error C2039: 'ceil' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(42) : error C2873: 'ceil' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(43) : error C2039: 'cos' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(43) : error C2873: 'cos' : symbol cannot be used in a using-declaration
c:\program files\microsoft visual studio 9.0\vc\include\cmath(43) : error C2039: 'cosh' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\cmath(43) : fatal error C1003: error count exceeds 100; stopping compilation
Aug 4, 2010 at 7:53am
Are you sure all these functions are in namespace std?

(because you seem to be using std::sqrt, and in the example in http://www.cplusplus.com/reference/clibrary/cmath/sqrt/ it uses sqrt without std::)
Last edited on Aug 4, 2010 at 7:55am
Aug 4, 2010 at 9:27am
They're supposed to be in namespace std.
Aug 5, 2010 at 1:54pm
I upgraded to VC++ 2010, and the problem seems to be solved.
Topic archived. No new replies allowed.