Recursion

Mar 28, 2010 at 12:35am
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);
	}
}
Mar 28, 2010 at 5:51am
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?

Mar 28, 2010 at 7:34am
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.
Mar 28, 2010 at 9:31pm
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.