たくさんの古いC++コードをMSVisualC++7.0からiOS4iPhone 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);
};
問題の2つの演算子はあいまいとしてマークされていますが、理由はわかりません。もともとこのクラスはテンプレートとして書かれていましたが、実際にはdouble型でのみインスタンス化されていました。そこで、上記の定義をもたらすcomplex_dクラスのテンプレートを解除しました。MS Visual C ++ .NET 2002を使用してMSC環境でエラーと警告なしでコンパイルしましたが、g++4.2.1でこれらのあいまいなエラーが発生します。
私はC++でオーバーロード演算子を使用してコードを書くのにかなり長い時間がかかり、*演算子の2つの定義を書き直すことをたくさん実験しました。主な問題は、これがあいまいな理由がわからないことです。にとって:
qSt * dVal
complex_dにdouble変数値を掛けて、結果をcomplex_dとして返す必要があります。したがって、フレンド演算子*を評価する必要があります。オペレーターを入れ替えると
friend complex_d operator*(const complex_d& lhs, double rhs);
と
complex_d operator*(double rhs);
クラスメンバーまたは列挙型がパラメーターとして必要であるという別のエラーが発生します。また、コード内の別の場所でも必要になるため、問題の2番目の演算子を省略することはできません。
このジレンマから抜け出す方法を教えてくれる人はいますか?