Math.h

Started by
9 comments, last by dalleboy 19 years, 2 months ago
Using Math.h in a simple 2D game, I am able to use functions like sin() and sqrt() without problems, but when trying to use the #defines created in the math.h header file I get the following errors: error C2065: 'M_PI' : undeclared identifier error C3861: 'M_PI': identifier not found, even with argument-dependent lookup math.h is included in a class .h file, but not the .cpp. I find copying the #defines from the math.h file to my class.h solves the problem, but it feels like I'm just working around a problem rather then solving it. I know it's probably something simple but I don't know what it is, as it seems like it should work. Hell, the text editor (VS.net 2003) pops up a tooltip with #define 3.14159... when I hover the mouse over it, and yet the compiler can't find it :(
Advertisement
#include <cmath> for C++.
Rob Loach [Website] [Projects] [Contact]
math.h is a valid c++ header (although it is deprecated), you will need to post the code in order to get a helpful response.
It's not standard in C, I don't know about C++ though.
However it's easy to workaround by defining it manually. Just place the following code in a header together with any other portability fixes.
#ifndef M_PI#define M_PI 3.1415926535897932384626433832795#endif
#define _USE_MATH_DEFINES


Dunno why they have done it this way...
cmath doesn't fix the problem.

#include <cmath>int main()	{	double x = sqrt(14.0);	double y = sin(2.0);	double z = M_PI/2;	return 0;	}

test.cpp(7): error C2065: 'M_PI' : undeclared identifier
ah thanks to the anonymous dude, the solution is this:

#define _USE_MATH_DEFINES#include <cmath>int main()	{	double x = sqrt(14.0);	double y = sin(2.0);	double z = M_PI/2;	return 0;	}


the #define must come before the #include.
Thanks guys
It was actually meant for math.h... :P
cmath includes math.h anyway :P
Quote:Original post by Dan Forever
ah thanks to the anonymous dude, the solution is this:

#define _USE_MATH_DEFINES#include <cmath>int main()	{	double x = sqrt(14.0);	double y = sin(2.0);	double z = M_PI/2;	return 0;	}


the #define must come before the #include.
Thanks guys


If you use the cmath header as you did in your example, then you must qualify the std namespace. std::sqrt and std::sin, or using namespace std. Your code as you pasted it should not compile.

This topic is closed to new replies.

Advertisement