Err... What is this error?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Err... What is this error?

Post 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.
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Err... What is this error?

Post 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...
Image ImageImage Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Err... What is this error?

Post 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.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: Err... What is this error?

Post by RandomDever »

Casting it to a float worked ismetteren. Thank you. But why doesn't it work with ints?
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Err... What is this error?

Post 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.
Image ImageImage Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Err... What is this error?

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: Err... What is this error?

Post 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.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Err... What is this error?

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: Err... What is this error?

Post by RandomDever »

Well that's microsoft for you. 8-)
Post Reply