'double' to 'fix' error

I'm simply trying to use the c library cosine and sine functions, but I am getting a compile error.
I'm using Visual C++ 2008. Here is the code and errors.

Any help will be greatly appreciated.

Speed, x, y and angle are all declared as floats.

1
2
3
4
	void move(){
		x += speed*cos(angle);
		y += speed*sin(angle);
	}



1
2
3
4
1>c:\users\gref\documents\visual studio 2008\projects\gl experiments\main.cpp(39) : error C2664: 'cos' : cannot convert parameter 1 from 'float' to 'fix'
1>        Constructor for class 'fix' is declared 'explicit'
1>c:\users\gref\documents\visual studio 2008\projects\gl experiments\main.cpp(40) : error C2664: 'sin' : cannot convert parameter 1 from 'float' to 'fix'
1>        Constructor for class 'fix' is declared 'explicit'
Last edited on
Are you including <math.h> in your project?

If so, this is the deprecated c header.

Try EITHER;

#include <cmath> // this is the C++ math header

OR

replace

cos(angle) with cosf(angle)
and
sin(angle) with sinf(angle)

as cosf() and sinf() take float as their parameters (your error message suggest that your cos() and sin() are attempting to typecast "angle" -- parameter 1 -- to something else -- this would be the case if you included the deprecated C header <math.h>)
Last edited on
PS...

It might be worth comparing the function definitions in <math.h> vs <cmath>;

for <math.h>

http://www.codecogs.com/reference/math.h/cos.php?alias=cos

for <cmath>

http://www.cplusplus.com/reference/clibrary/cmath/cos.html

notice how C++ is a bit smarter - it will return float, double or long depending upon the parameter you send to cos()/sin(). In C (with <math.h>) you had to use cosf()/sinf(), cos()/sin() or cosl()/sinl() respectively.

hope it helps purge the gremlins...
Thanks for the help.
Topic archived. No new replies allowed.