Ive got a problem that I cant use operator overloads even though I declared them. I've got a class CKomplex for complex numbers:
typedef double number;
class CKomplex
{
private: // member variables
number mReal;
number mImag;
public:
CKomplex(number real = 0);
CKomplex(number real, number imag);
CKomplex(number radius, number phi, bool isRad);
// Operator
void operator+=(CKomplex &rhs);
void operator-=(CKomplex &rhs);
void operator*=(CKomplex &rhs);
void operator/=(CKomplex &rhs);
void operator=(CKomplex &rhs); // =
friend CKomplex operator+(CKomplex &lhs, CKomplex &rhs); // +
friend CKomplex operator-(CKomplex &lhs, CKomplex &rhs);
friend CKomplex operator*(CKomplex &lhs, CKomplex &rhs);
friend CKomplex operator/(CKomplex &lhs, CKomplex &rhs);
// Get & Setter
number GetReal() { return mReal; }
void SetReal(number real) { mReal = real; }
number GetImag() { return mImag; }
void SetImag(number imag) { mImag = imag; }
}
// Here is the implementation
void CKomplex::operator=(CKomplex &rhs) { // no friend
mReal = rhs.GetReal();
mImag = rhs.GetImag();
}
CKomplex operator+(CKomplex &lhs, CKomplex &rhs) { // friend
number real = lhs.GetReal() + rhs.GetReal();
number imag = lhs.GetImag() + rhs.GetImag();
return CKomplex(real, imag);
}
But the code wont compile when I write this in my main() function:
CKomplex z1 = CKomplex(5,3); // works
CKomplex z2 = CKomplex(8,5); // works
CKomplex z3 = z1 + z3; // works; but why?
z3 = z1; // works
z3 = z1 + z2; // doesnt work line 30
z3 = (z1 + z2); // doesnt work also
With the error:
C:\Users\User\Documents\soExample\main.cpp:30: Fehler: no match for 'operator=' (operand types are 'CKomplex' and 'CKomplex')
z3 = z1 + z2;
^
(Arrow at the =)
Its my first C++ program, Im using QT 5.6.0 as console application on Windows 7.
If you need further information feel free to ask, I also can give you the whole source code.
Why does line 3 compile but not line 6 in my main()? Where is the difference for the operators?