1

我现在几乎没有想法将一大堆旧的 C++ 代码从 MS Visual C++ 7.0 移植到 iOS 4 iPhone g++ 4.2.1 编译器。我在编译这个时遇到了一些模棱两可的错误:

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

有问题的两个运算符被标记为模棱两可,但我不明白为什么。最初这个类是作为模板编写的,实际上只是用类型实例化。所以我取消了导致上述定义的complex_d类的模板。它使用 MS Visual C++ .NET 2002 在 MSC 环境中编译没有错误和警告,但我现在使用 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);

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

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

4

1 回答 1

3

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

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

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

  2. 删除友元运算符*()。

C++ std::lib 与解决方案 #2 一起用于 std::complex。

于 2011-04-02T20:40:29.957 回答