pow function is giving me an error?

I'm creating a program that converts binary to decimal. For some reason the pow function isn't working... It keeps saying "more than one instance of overloaded function "pow" matches the argument list:"

Here is the code. Someone please help me!

double deci;
string bin;
int len;

cout << "Enter a binary number: " << endl;
cin >> bin;

cout << bin << endl;

len = bin.length();

cout << len << endl;
deci = 0;
for (int i=0; i<len; i++)
if (bin.at(i) == '1')
deci = deci + pow(2, len-i-1);
The short answer is RTFM :)

Let's look up the function pow:
http://en.cppreference.com/w/cpp/numeric/math/pow

Now, you're probably not using C++11, so you've got these pow functions:
1
2
3
float       pow( float base, float exp )
double      pow( double base, double exp );
long double pow( long double base, long double exp );


There's one that takes two float objects as inputs, and one that takes two double objects, and one that takes two long double objects.

So let's see which one you're trying to use; you're passing in 2, which is an int, and len-i-1, which is an int. So you're trying to pass in two int objects.

There isn't one that takes two int objects. The compiler is complaining because it doesn't know which function you want to use. It knows how to change an int to a float or a double or a long double, but it has a choice of three options and no idea which one you mean.

Last edited on
So, how would I go about configuring this to work? I really have no idea =(
Thank you for making that clear though.

EDIT: So I changed it up a bit and put len as a double and it ended up not giving me an error, but I thought passing "2" was an int so why is it working?
Last edited on
read what he posted and u'll see that u have to change int i to float i and int len to float len
so why is it working?


Presumably, by making len a double, you have make it clear that double pow( double base, double exp ); is the one you want to use.
The compiler only needs enough information to resolve the ambiguity.

You might also have done something like this:
pow((double) 2, len-i-1);
or better, this:
pow(2.0, len-i-1);


Topic archived. No new replies allowed.