Simple Help Please

For some reason, this code:
1
2
3
4
int ball::collision_detection(obstacle_ball &OBSTACLE, char return_x_or_y)
{
	int distance = std::sqrt( (std::pow( (OBSTACLE.get_obstacle_ball_x_position() - ball_x_position) , 2 )) + (std::pow( (OBSTACLE.get_obstacle_ball_y_position() - ball_y_position) , 2 )) );	
}


is giving me the error


1>------ Build started: Project: ALLEGRO_TEST, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\matt cromer\documents\visual studio 2010\projects\allegro_test\allegro_test\ball.h(203): error C2668: 'pow' : ambiguous call to overloaded function
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): could be 'double pow(double,int)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or       'float pow(float,int)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): or       'long double pow(long double,int)'
1>          while trying to match the argument list '(int, int)'
1>c:\users\matt cromer\documents\visual studio 2010\projects\allegro_test\allegro_test\ball.h(203): error C2668: 'pow' : ambiguous call to overloaded function
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): could be 'double pow(double,int)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or       'float pow(float,int)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): or       'long double pow(long double,int)'
1>          while trying to match the argument list '(int, int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I'm just trying to use the distance formula for the coordinates of the balls.
All of the variables I am calling are integers... what am I doing wrong?
I am also new to c++, but it looks like you need to typecast your first variable into either, double, float or long double in order for it to match one of the overloaded functions.
Where do I need to typecast? Could you post an example of it real quick?
Try it like this:

1
2
3
4
int ball::collision_detection(obstacle_ball &OBSTACLE, char return_x_or_y)
{
	int distance = std::sqrt( (std::pow((double) (OBSTACLE.get_obstacle_ball_x_position() - ball_x_position) , 2 )) + (std::pow((double) (OBSTACLE.get_obstacle_ball_y_position() - ball_y_position) , 2 )) );	
}
Ahhh I see, thanks alot.

I was confused, because I thought that the compiler would check the variable types and be like okay, they're good already! And I was confused about the syntax of doing it. I thought it went infront of pow.

Again, thanks alot.
I am glad I could help. Also note that, return value will be whatever you cast your first argument to be.
Topic archived. No new replies allowed.