Ambiguous call

  • Thread starter Thread starter Anna Smidt
  • Start date Start date
A

Anna Smidt

lf.lfHeight = - (int) (fabs (pt.y) / 10.0 + 0.5) ;

Error 1 error C2668: 'fabs' : ambiguous call to overloaded function
ezfont.cpp 47 stasm

I first thought that the compiler wants to fight with me because there
where some decimals missing, but it's already .0 and .5.

Also here an error "ambiguous call" is thrown...

lf.lfWidth = (int) (tm.tmAveCharWidth *
fabs (pt.x) / fabs (pt.y) + 0.5) ;


I understand that it's because it's a C function that I have converted
to C++ (at least I think so), but I don't know how to get it right.
Anna.
 
Anna Smidt said:
lf.lfHeight = - (int) (fabs (pt.y) / 10.0 + 0.5) ;

Error 1 error C2668: 'fabs' : ambiguous call to overloaded function
ezfont.cpp 47 stasm

I first thought that the compiler wants to fight with me because
there where some decimals missing, but it's already .0 and .5.

Also here an error "ambiguous call" is thrown...

lf.lfWidth = (int) (tm.tmAveCharWidth *
fabs (pt.x) / fabs (pt.y) + 0.5) ;


I understand that it's because it's a C function that I have
converted to C++ (at least I think so), but I don't know how to get
it right. Anna.

See the solution (bottom example) in the documentation on C2668.
http://msdn.microsoft.com/en-us/library/da60x087(VS.80).aspx

That means, explicitly do the conversion on your own to make it unambiguous
for the compiler:

....(float) fabs (pt.x)...


Is it required to convert the file to C++? You can set the language (C/C++)
and other settings on a per file basis. (however, I don't know what problems
can occur when the linker comes into play...)


Armin
 
Hi Armin,

I changed

lf.lfHeight = - (int) (fabs (pt.y) / 10.0 + 0.5) ;

to

lf.lfHeight = - (int) (float) (fabs (pt.y) / 10.0 + 0.5)
;//MODIFIED BY ANNA:ORIG is without (float)

....and still have that error.

I have only 3 of these errors, so I will change it manually and not set
compiler options on a file-base if I can't help it.

Thanks!
Anna
 
lf.lfHeight = - (int) (fabs (pt.y) / 10.0 + 0.5) ;

Error 1 error C2668: 'fabs' : ambiguous call to overloaded function
ezfont.cpp 47 stasm

I first thought that the compiler wants to fight with me because there
where some decimals missing, but it's already .0 and .5.

But you're not passing them to fabs.
Also here an error "ambiguous call" is thrown...

lf.lfWidth = (int) (tm.tmAveCharWidth *
fabs (pt.x) / fabs (pt.y) + 0.5) ;


I understand that it's because it's a C function that I have converted
to C++ (at least I think so), but I don't know how to get it right.
Anna.

When you get an error like this, copy and paste the entire error message
into your post, because IIRC, the compiler will indicate the overloads
involved in the ambiguity. Also specify the types of pt.x and pt.y. Doing
this will let someone definitively answer your question. I'm going to guess
that pt is a Windows POINT, and thus pt.x and pt.y are LONG, and there are
(at least) fabs(float) and fabs(double) overloads. If this is correct, you
can just cast to double to resolve the ambiguity, e.g.
fabs(static_cast<double>(pt.y)).
 
Anna Smidt said:
Hi Armin,

I changed

lf.lfHeight = - (int) (fabs (pt.y) / 10.0 + 0.5) ;

to

lf.lfHeight = - (int) (float) (fabs (pt.y) / 10.0 + 0.5)
;//MODIFIED BY ANNA:ORIG is without (float)

...and still have that error.

I have only 3 of these errors, so I will change it manually and not
set compiler options on a file-base if I can't help it.

Thanks!
Anna

Sorry, my fault. But obviously you didn't understand the meaning of the
error message. ;-) pt.y is an integer, I guess, and none of the overloads
accepts an integer. Therefore, pt.y must be converted:

....fabs((float) pt.y)...


Armin
 
Sorry, my fault. But obviously you didn't understand the meaning of the
error message. ;-) pt.y is an integer, I guess, and none of the overloads
accepts an integer. Therefore, pt.y must be converted:

...fabs((float) pt.y)...

Casting a 32 bit integer to a 32 bit float may lose precision and may even
throw a structured exception (I don't recall if this is masked by default
or not). Basically, the only reason to ever use float is to save space, and
casting to double is the right choice here.
 
Doug Harrison said:
Casting a 32 bit integer to a 32 bit float may lose precision and
may even throw a structured exception (I don't recall if this is
masked by default or not). Basically, the only reason to ever use
float is to save space, and casting to double is the right choice
here.

Just wanted to show "how to", and as the final result is int anyway, it
shouldn't matter much. But in general you're right.
Maybe I'm sometimes still on the float = 4 bytes = native = fastest trip,
which is outdated, of course. ;-)


Armin
 
Doug said:
But you're not passing them to fabs.

No, so why use fabs in the first place? :-)

In C++ you can just use std::abs and the compiler will select the
proper overload for you.

On the other hand, why convert to float just to get a rounded divide
by 10?

Using (abs(pt.y) + 5) / 10 will do just as well.


Bo Persson
 
No, so why use fabs in the first place? :-)

Now that's a good point. :)
In C++ you can just use std::abs and the compiler will select the
proper overload for you.

On the other hand, why convert to float just to get a rounded divide
by 10?

Using (abs(pt.y) + 5) / 10 will do just as well.

That's susceptible to overflow in the addition, but in this context,
calculating a font height, I agree with you.
 
Back
Top