warning: ISO C++ says that these are ambiguous

Discussion in 'C++' started by TheFlyingDutchman, May 13, 2011.

  1. 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.
     
    TheFlyingDutchman, May 13, 2011
    #1
    1. Advertising

  2. 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 .
     
    Johannes Schaub, May 13, 2011
    #2
    1. Advertising

  3. TheFlyingDutchman

    Qi Guest

    On 2011-5-13 17:12, TheFlyingDutchman wrote:
    > ClassB(double&)


    Change the constructor to

    explicit ClassB(double &);

    to prevent implicit type conversion?


    --
    WQ
     
    Qi, May 13, 2011
    #3
  4. On May 13, 3:23 am, Qi <> wrote:
    > On 2011-5-13 17:12, TheFlyingDutchman wrote:
    >
    > > ClassB(double&)

    >
    > Change the constructor to
    >
    > explicit ClassB(double &);
    >
    > to prevent implicit type conversion?
    >
    > --
    > WQ


    Yes, that works. Thanks!
     
    TheFlyingDutchman, May 13, 2011
    #4
  5. On May 13, 2:44 am, Johannes Schaub <>
    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);}


    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.
     
    TheFlyingDutchman, May 13, 2011
    #5
  6. TheFlyingDutchman wrote:

    > On May 13, 2:44 am, Johannes Schaub <>
    > 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.
     
    Johannes Schaub, May 13, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franck DARRAS
    Replies:
    12
    Views:
    659
    Jim Higson
    Aug 23, 2004
  2. Alexei Polkhanov
    Replies:
    11
    Views:
    2,480
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,072
    Peter Koch Larsen
    Nov 26, 2004
  4. Replies:
    3
    Views:
    465
    Jerry Coffin
    Sep 2, 2006
  5. Julian Mehnle
    Replies:
    17
    Views:
    933
    Julian Mehnle
    May 18, 2006
Loading...

Share This Page