c++ - 如何解决 ISO C++ 中不明确的运算符

标签 c++ ios4 operator-overloading iso ambiguous

我现在完全没有想法将一大堆旧 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);
};

有问题的两个运算符被标记为不明确,但我不明白为什么。最初这个类被写成一个模板,实际上只是用 double 类型实例化。因此,我取消了 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);

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

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

最佳答案

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

  1. 在构造函数中显式添加:

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

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

C++ std::lib 使用 std::complex 的解决方案 #2。

关于c++ - 如何解决 ISO C++ 中不明确的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5525625/

相关文章:

c++ - 错误 C4716 : 'operator<<' : must return a value

c++ - 重载 << 以定义操纵器

c++ - 在 gui 中处理子级到父级鼠标和键盘事件的最佳方法?

c++ - 搜索节点 - Libxml

objective-c - 在不使用导航 Controller 的情况下在选项卡栏中的同一选项卡上切换 View

android - 检测来自移动应用程序的 http 请求

ruby - 在 Ruby 中,我可以覆盖三元运算符 '?'

c++ - 如何将 GUI C++ 应用程序转换为控制台应用程序?

c++ - hash_map 问题/教程

iphone - 如何用项目构建ZBar SDK而不是使用静态库?