Recursion

Hey everyone, I am trying to convert a binary number to decimal form but I keep getting an error when trying to use the pow function in cmath. The compiler is telling me that is an ambiguous call to an overloaded function. Can anyone tell me what I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
void binaryToDecimal(int binaryNumber, int& decimal, int& weight)
{
	int bit;
	if(binaryNumber > 0)
	{
		bit = binaryNumber % 10;
		decimal = decimal + bit * static_cast<int>(pow(2,weight));
		binaryNumber = binaryNumber / 10;
		weight++;
		binaryToDecimal(binaryNumber,decimal,weight);
	}
}
http://www.cplusplus.com/reference/clibrary/cmath/pow/

The example code shows the following at the top of the source code:

#include <math.h>

Is it complaining because you have pow() defined in multiple headers that you are importing?

No, the problem is because pow(int, int) is not defined, so ints could either be converted to float or double. To fix the problem, cast one of the arguments to a float or a double.
Yeah that solved the problem, thanks for the help!

 
decimal = decimal + bit * static_cast<int>(pow(2,static_cast<float>(weight)));
Topic archived. No new replies allowed.