我現在幾乎沒有任何想法,將一大堆舊的C++代碼從MS Visual C++ 7.0移植到iOS 4 iPhone g ++ 4.2.1編譯器。我得到一些ambiquity錯誤編譯如下:如何解決ISO C++中的歧義運算符
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);
};
的兩家運營商在問題被標記爲ambigous,但我不明白爲什麼。本來這個類是作爲模板寫的,實際上只是用double類型實例化。所以我對模板進行了取樣,導致上面的定義。它使用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:標準庫已經提供了一個複雜的類。如果你需要這個功能,至少要建立在已有的功能之上。 – rubenvb 2011-04-02 21:37:44
非常感謝霍華德:-) 我已經試過但只有2.作品。我也嘗試過,但我不確定是否可以簡單地省略朋友操作員。所以我現在很好。 @rubenvb:哇,我沒有搜索過STL - 在MFC中,我總是將STL包裝成MFC類。在端口到g ++ 4.2.1的過程中,我現在使用更多的STL代碼,我根本不知道已經有一個複雜的類。只要它與我自己的班級合作,我想我不會重寫它。但是,無論如何要指出我:-) – konran 2011-04-02 22:13:16