warning: ISO C++ says that these are ambiguous

T

TheFlyingDutchman

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.
 
J

Johannes Schaub

TheFlyingDutchman said:
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 .
 
T

TheFlyingDutchman

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);}


I found that I can get rid of the warning by either eliminating the
extra operator+ with the ClassB input or by using "explicit" on the
ClassB constructor as someone suggested.
 
J

Johannes Schaub

TheFlyingDutchman said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top