Mega Search
23.2 Million


Sign Up

Make a donation  
BCC32 doesn't know a float from an int  
News Group: borland.public.cppbuilder.language.cpp

Hi Folks

Compiler version: Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 
Borland

Can someone please explain why the following program does not compile:

= begin tryit.cpp =======================
class MyType
{
public:
   MyType& operator= ( const float rhs );
   MyType& operator= ( const int rhs );

private:
   float internal_val;
};

MyType& MyType::operator= ( const float rhs )
{
   internal_val = rhs;
   return *this;
}

MyType& MyType::operator= ( const int rhs )
{
   internal_val = static_cast< float >( rhs );
   return *this;
}

int main()
{
   MyType   TryThis;

   // The following line compiles
   TryThis = 2;

   // This following line produces the error:
   // Error E2015 tryit.cpp 32: Ambiguity between 'MyType::operator =(const 
float)' and
   // 'MyType::operator =(const int)' in function main()
   TryThis = 1.5;
}
= end tryit.cpp =========================

To me it seems like BCC32 thinks the value '1.5' can be interpreted as an 
integer.

TIA
Ebbe 



Vote for best question.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 3:50 PM EST
From: Ebbe Kristensen
 
Re: BCC32 doesn't know a float from an int  
News Group: borland.public.cppbuilder.language.cpp
"Ebbe Kristensen"  wrote:

>I find it rather counter-intuitive that a double-to-int conversion has the
>same priority as a double-to-float conversion. 

Both cases require information to be thrown away. Your intuition may say
that more has to be thrown away for the int, but the compiler just notes
there are two possibilities, and both have this problem.

Alan Bellingham
-- 
Team Browns
 Borland newsgroup descriptions
      netiquette

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 9:10 AM EST
From: Alan Bellingham
 
Re: BCC32 doesn't know a float from an int  
News Group: borland.public.cppbuilder.language.cpp
Ed Mulroy [TeamB] wrote:
> You called it with a double.  There is one step to convert to an int
> and one step to convert to a float so neither conversion is
> preferable.

I find it rather counter-intuitive that a double-to-int conversion has the
same priority as a double-to-float conversion. I haven't found anything
about this in the C++ Builder help files (no, the section "Standard
arithmetic conversions" in bcb6lang.hlp does not tell) nor in "The C++
Programming Language" (3rd ed) but that may be because I haven't searched
for the right keywords. Using keywords such as conversion, implicit,
parameter, overload
or argument did not yield anything.

However your explanation did help me towards a solution (added operator=
(double& rhs); ) so I owe you a big thank you for that :-)

Ebbe








Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Jan-2008, at 9:16 AM EST
From: Ebbe Kristensen
 
Re: BCC32 doesn't know a float from an int  
News Group: borland.public.cppbuilder.language.cpp
You called it with a double.  There is one step to convert to an int and one 
step to convert to a float so neither conversion is preferable.

..  Ed

> Ebbe Kristensen wrote in message
> news:4784df4e@newsgroups.borland.com...
>
> Compiler version: Borland C++ 5.6.4 for Win32 ...Borland
>
> Can someone please explain why the following program does not compile:
>
> = begin tryit.cpp =======================
> class MyType
> {
> public:
>   MyType& operator= ( const float rhs );
>   MyType& operator= ( const int rhs );
>
> private:
>   float internal_val;
> };
>
> MyType& MyType::operator= ( const float rhs )
> {
>   internal_val = rhs;
>   return *this;
> }
>
> MyType& MyType::operator= ( const int rhs )
> {
>   internal_val = static_cast< float >( rhs );
>   return *this;
> }
>
> int main()
> {
>   MyType   TryThis;
>
>   // The following line compiles
>   TryThis = 2;
>
>   // This following line produces the error:
>   // Error E2015 tryit.cpp 32: Ambiguity between 'MyType::operator =(const 
> float)' and
>   // 'MyType::operator =(const int)' in function main()
>   TryThis = 1.5;
> }
> = end tryit.cpp =========================
>
> To me it seems like BCC32 thinks the value '1.5' can be interpreted as an 
> integer.



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Jan-2008, at 10:00 AM EST
From: Ed Mulroy [TeamB]