The Code based on your post (see bottom of this post) compiles and
works. However other previous working code now fails - and I do not
understand why...
The following class (with #include <MpMath.h>)
class CComplexVector2
{
public:
/* Konstruktoren */
inline CComplexVector2() { m[1]=&x;m[2]=&y; x.re=y.re=x.im=y.im=0.0; }
inline CComplexVector2(const CComplexVector2 &c) { m[1]=&x;m[2]=&y;
x=c.x;y=c.y;}
inline CComplexVector2(const double &d1, const double &d2) {
m[1]=&x;m[2]=&y; x.re=d1;y.re=d2;x.im=y.im=0.0;}
inline CComplexVector2(const CComplex &d1, const CComplex &d2) {
m[1]=&x;m[2]=&y; x=d1;y=d2;}
/* Operatoren */
inline CComplexVector2& operator=(const CComplexVector2 &c) {
m[1]=&x;m[2]=&y; x=c.x;y=c.y; return *this;}
inline CComplexVector2 operator+(CComplexVector2 c) { return
CComplexVector2(x+c.x,y+c.y);}
inline CComplexVector2 operator+(CVector2 c) { return
CComplexVector2(x+c.x,y+c.y);}
CComplex x,y;
CComplex *m[3];
};
fails with
>D:\Matthias\CPP\Uwe\C++\LightModulator2D\LightMod ulator2D\um.h(185) :
error C2678: Binärer Operator '+': Es konnte kein Operator gefunden
werden, der einen linksseitigen Operanden vom Typ 'CComplex' akzeptiert
(oder keine geeignete Konvertierung möglich)
1>
D:\Matthias\CPP\Uwe\C++\LightModulator2D\LightModu lator2D\MpMath.h(51):
kann 'CComplex operator +(double,const CComplex &)' sein
1>
D:\Matthias\CPP\Uwe\C++\LightModulator2D\LightModu lator2D\MpMath.h(52):
oder "CComplex operator +(const CComplex &,double)"
1 bei Anpassung der Argumentliste '(CComplex, CComplex)'
The CComplex class now looks like (MpMath.cpp)
class CComplex
{
public:
/* Überladen Konstruktoren */
CComplex(): re(0), im(0) {}
//CComplex(const double &d, const double &c): re(d), im(c) {}
CComplex(const double &d1, const double &d2) {re=d1;im=d2;}
CComplex(const CComplex &c) { re=c.re;im=c.im;}
CComplex(const double &c) { re=c;im=0.0;}
/* Multiplikation */
CComplex & operator *=(CComplex const & c){re = re*c.re-im*c.im; im =
re*c.im+im*c.re; return *this;}
CComplex & operator *=(double d) {re *= d;im *= d; return *this;}
/* Addition */
CComplex & operator +=(CComplex const &c) {re=re+c.re; im=im+c.im;
return *this;}
CComplex & operator +=(double d) {re+=d; return *this;}
/* Subtraction */
CComplex & operator -=(CComplex const &c) {re= re-c.re; im=im-c.im;
return *this;}
CComplex & operator -=(double d) {re-=d; return *this;}
/* Division */
CComplex & operator /=(CComplex const &c) {
double b=c.re*c.re+c.im*c.im;
re=(re*c.re+im*c.im)/b;im=(im*c.re-re*c.im)/b; return *this;
};
CComplex & operator /=(double d){re=re/d;im=im/d;return *this;}
/* Invers */
CComplex operator -() { return CComplex(-re,-im);}
public:
double re;
double im;
};
// binary operators outside the class
// Necessary to have double * complex AND complex * double
inline CComplex operator*(double d, CComplex const & c){CComplex
ret(c);ret *= d;return ret;}
inline CComplex operator*(CComplex const & c, double d){return (d*c);}
inline CComplex operator+(double d, CComplex const & c){CComplex
ret(c);ret += d;return ret;}
inline CComplex operator+(CComplex const & c, double d){return (d+c);}
inline CComplex operator-(double d, CComplex const & c){CComplex
ret(c);ret -= d;return ret;}
inline CComplex operator-(CComplex const & c, double d){return (d-c);}
inline CComplex operator/(double d, CComplex const & c){CComplex
ret(c);ret /= d;return ret;}
inline CComplex operator/(CComplex const & c, double d){return (d/c);}