Page 1 of 1

Err... What is this error?

Posted: Fri Dec 16, 2011 9:55 am
by RandomDever

Code: Select all

'pow' : ambiguous call to overloaded function
Why.
Just Why.
No Honestly.
Why is it doing this...
But seriously. This is all that's standing between me and a brand new 'itoa' function.
And before you ask, the reason for me making a custom function to do something C++ already has a function for?
Because it doesn't work. Don't ask me why or how, it just doesn't.
Any help will be appreciated.

Re: Err... What is this error?

Posted: Fri Dec 16, 2011 10:06 am
by ismetteren
You should post the code which causes the problem. After spending two minutes on google it turns out that you should cast the first argument to a float, double or long double.

http://www.dreamincode.net/forums/topic ... -function/

But then again, without seeing the code, I can't know for sure...

Re: Err... What is this error?

Posted: Fri Dec 16, 2011 11:02 am
by XianForce
Instead of including "math.h", use "#include <cmath>" . That seemed to be the most common issue for this error :). But, next time, post some relevant code with your question, else it's near impossible to find the issue.

Re: Err... What is this error?

Posted: Fri Dec 16, 2011 11:38 am
by RandomDever
Casting it to a float worked ismetteren. Thank you. But why doesn't it work with ints?

Re: Err... What is this error?

Posted: Fri Dec 16, 2011 2:02 pm
by ismetteren
I'm not 100% sure on this, but it has something to do with the fact that pow is not defined for an interger base:

Code: Select all

     double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
      float pow (       float base,       float exponent );
     double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );
To me it seems like it should complain about the type of the base, but i guess it is trying to do some kind of implicit type conversion and can't figure out if it should convert to double or long double since both would work. By casting it to float, the only possibility there is, is to implicitly cast the exponent to a float too, and therefore there is no ambiguity.

This is just my guess though(with inspiration taken from the answer to the forum post i linked), I'm not sure if this is the correct explanation.

Re: Err... What is this error?

Posted: Sat Dec 24, 2011 6:34 am
by Ginto8
ismetteren is exactly right. When you call an overloaded function, the compiler goes through these steps (or something very similar):
1) if there is an exact match (name + parameters), call it
2) if there is a partial match (name + implicitly convertible parameters), call the best matching one
3) throw an error otherwise

In the situation you're describing, it's getting hung up at number 2 because ints can just as easily convert to floats as to doubles, and with floating point, precision matters, so it doesn't want to take the chance of just picking one, because it may be completely unsuitable for what you're doing.

Re: Err... What is this error?

Posted: Sun Jan 01, 2012 11:24 pm
by RandomDever
As it turns out I didn't need to make a custom function after all because I thought itoa() was failing.
But it was just me. I forgot about scope. Yeah... just yeah.
For anyone new to C++ reading this, remember, never make a function return a pointer to a local variable. It will EPIC FAIL.

Re: Err... What is this error?

Posted: Mon Jan 02, 2012 12:30 pm
by short
RandomDever wrote:As it turns out I didn't need to make a custom function after all because I thought itoa() was failing.
But it was just me. I forgot about scope. Yeah... just yeah.
For anyone new to C++ reading this, remember, never make a function return a pointer to a local variable. It will EPIC FAIL.
A good compiler will warn you about this.

Re: Err... What is this error?

Posted: Sun Jan 08, 2012 7:53 am
by RandomDever
Well that's microsoft for you. 8-)