开发者

How to resolve ambiguous operator in ISO C++

开发者 https://www.devze.com 2023-02-21 20:58 出处:网络
I\'m pretty much out of ideas now with porting a big bunch of old C++ code from MS Visual C++ 7.0 to iOS 4 iPhone g++ 4.2.1 compiler. I get some ambiquity errors compiling this:

I'm pretty much out of ideas now with porting a big bunch of old C++ code from MS Visual C++ 7.0 to iOS 4 iPhone g++ 4.2.1 compiler. I get some ambiquity errors compiling this:

complex_d*  cp;
complex_d   qSt;
double      zi;

// complex_d += complex_d * double
*cp += qSt * dVal;  // ISO C++ says that these are ambiguous

with the class definition of complex_d as:

#include <math.h>
#include "CObject.h"    // emulated MFC class, used for some types like BOOL
#include "CString.h"    // emulated MFC class, also needed for some types not on iOS

//  interface for complex calculations
//
/////////////////////////////////////////////////////////////////////////////

class polar_d;  // forward declaration

class complex_d
{
// attributes
protected:
    double  re;
    double  im;

// construction
public:
    complex_d(double re = 0, double im = 0);
    complex_d(const complex_d& x);
    virtual ~complex_d() { };

// implementat开发者_C百科ion
public:
    double  real(void) const;
    double  imag(void) const;
    double& setReal(void);      // needed because we don't have Serialize() here
    double& setImag(void);      // as above

    double  Abs(void) const;
    double  Phi(void) const;

    complex_d   Conjugate(void);
    polar_d     Polar(void);

    BOOL    IsZero(void) const;
    BOOL    IsReal(void) const;
    BOOL    IsImag(void) const;

    complex_d& operator=(const complex_d& rhs);
    complex_d& operator+=(const complex_d& rhs);
    complex_d& operator-=(const complex_d& rhs);
    complex_d& operator*=(const complex_d& rhs);
    complex_d& operator/=(const complex_d& rhs);

    complex_d operator+(const complex_d& rhs);
    complex_d operator-(const complex_d& rhs);
    complex_d operator*(const complex_d& rhs);  // ambiguous error here...
    complex_d operator/(const complex_d& rhs);

    complex_d operator-(void);          // unary

    complex_d& operator=(const double& rhs);

    friend complex_d operator+(const complex_d& lhs, double rhs);
    friend complex_d operator+(double lhs, const complex_d& rhs);
    friend complex_d operator-(const complex_d& lhs, double rhs);
    friend complex_d operator-(double lhs, const complex_d& rhs);
    friend complex_d operator*(const complex_d& lhs, double rhs);   // ... and here also ambigous
    friend complex_d operator*(double lhs, const complex_d& rhs);
    friend complex_d operator/(const complex_d& lhs, double rhs);
    friend complex_d operator/(double lhs, const complex_d& rhs);
    friend BOOL operator==(const complex_d& lhs, double rhs);
    friend BOOL operator==(double lhs, const complex_d& rhs);
    friend BOOL operator!=(const complex_d& lhs, double rhs);
    friend BOOL operator!=(double lhs, const complex_d& rhs);

    friend BOOL operator==(const complex_d& lhs, const complex_d& rhs);
    friend BOOL operator!=(const complex_d& lhs, const complex_d& rhs);
};

The two operators in question are marked as ambigous but I don't see why. Originally this class was written as a template which in fact only was instantiated with double type. So I de-templated the complex_d class which results in the definition above. It compiled w/out errors and warnings in MSC environment using MS Visual C++ .NET 2002 but I get these ambiguity errors with g++ 4.2.1 now.

I'm quite long off writing code with overloading operators in C++ and I experimented a lot rewriting the two definitions of the * operators. The main problem is I don't understand why this is ambiguous. For:

qSt * dVal

a complex_d has to be multiplied with a double variable value and the result has to be returned as complex_d. Therefore the friend operator * has to be evaluated. When I replace the operator

friend complex_d operator*(const complex_d& lhs, double rhs);

with

complex_d operator*(double rhs);

I get another error telling me that a class member or enum is needed as parameter. It is also not possible to omit the second operator in question because it is also needed at another place in the code.

Is anyone out there who can tell me how to get out of this dilemma?


I see two ways to fix this (there's probably more):

  1. Add explicit to the constructor:

    explicit complex_d(double re = 0, double im = 0);

  2. Remove the friend operator*().

The C++ std::lib went with solution #2 for std::complex.

0

精彩评论

暂无评论...
验证码 换一张
取 消