如何解决ISO C ++中的模糊运算符

时间:2011-04-02 20:21:41

标签: c++ ios4 operator-overloading iso ambiguous

我现在几乎没有想法将大量旧的C ++代码从MS Visual C ++ 7.0移植到iOS 4 iPhone g ++ 4.2.1编译器。我得到了一些编码的ambiquity错误:

complex_d*  cp;
complex_d   qSt;
double      zi;

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

complex_d 的类定义为:

#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() { };

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

有问题的两名经营者被标记为暧昧但我不明白为什么。最初这个类是作为模板编写的,实际上只是用 double 类型实例化。所以我去了 complex_d 类,这导致了上面的定义。它使用MS Visual C ++ .NET 2002在MSC环境中编译了w / out错误和警告,但我现在用g ++ 4.2.1得到了这些歧义错误。

我在C ++中使用重载运算符编写代码已经很久了,我尝试了很多重写 * 运算符的两个定义。主要问题是我不明白为什么这是模棱两可的。为:

qSt * dVal

complex_d必须与double变量值相乘,结果必须作为complex_d返回。因此,必须评估朋友运营商* 。当我更换操作员

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

complex_d operator*(double rhs);

我收到另一个错误,告诉我需要一个类成员或枚举作为参数。也无法省略有问题的第二个运算符,因为代码中的其他位置也需要它。

有谁可以告诉我如何摆脱这种困境?

1 个答案:

答案 0 :(得分:3)

我看到两种解决方法(可能还有更多):

  1. 向构造函数添加显式:

    显式complex_d(double re = 0,double im = 0);

  2. 删除朋友操作员*()。

  3. C ++ std :: lib与std :: complex的解决方案#2一起使用。