Re: warning: ISO C++ says that these are ambiguous
TheFlyingDutchman wrote:
On May 13, 2:44 am, Johannes Schaub <schaub.johan...@googlemail.com>
wrote:
TheFlyingDutchman wrote:
given ClassA methods:
ClassA operator+(const long& ) const;
ClassA operator+(const int& ) const;
ClassA operator+(const double& ) const;
ClassA operator+(const ClassB&) const;
I get a warning:
warning: ISO C++ says that these are ambiguous even though the worst
conversion for the first is better than the worst conversion for the
second
I can get rid of the warning if I remove the ClassB constructor that
takes a double:
ClassB(double &);
But I would prefer to leave it in if there was some other way to clear
up the ambiguity.
You have given too little information. You hit a case similar to
this:http://stackoverflow.com/q/3519282/34509.
I am not having any luck reproducing it with a minimum subset of the
code. I did find out that I had another statement
ClassA operator+(const ClassB&);
in addition to
ClassA operator+(const ClassB&) const;
The warning occurs on:
ClassA operator++(int) { ClassA temp = *this;
*this = temp + (long) 1; return (temp);}
If "ClassB" logically represents a double, then making its constructor
"explicit" is not a good idea at all.
You need to fix the underlying issue. Having a non-const "operator+" is not
good, so make it const or remove it, because you already have a const
version of it.
You also have a problem with your compiler, because it accepts that you pass
a long prvalue to a class constructor that has a non-const reference as a
parameter.
Stop, rethink, and then redesign these overloads. First understand what the
warning means exactly, and then try to fix it. The linked SO question's
answer explains what is wrong.