I have written the following code to do operator overloading for basic operations on complex numbers, here I have put only the addition. When running it, I get trillions of errors. I have seen several other posts regarding this but I have just started learning C++. I would be most grateful if you could point out in general the mistakes I am making in operator overloading (It is not about debugging, but I would appreciate some general explanations as I have seen examples of the same operator overloading but could not understand several features such as the use of pointer and const). Thanks in advance.
#include<iostream>
using namespace std;
class Complex
{
private:
double real;
double imaginary;
public:
Complex(double r, double i);
// addition
Complex operator+(Complex c1, Complex c2);
};
Complex::Complex(double r, double i)
{
real = r;
imaginary = i;
}
Complex Complex::operator+(Complex c1, Complex c2)
{
Complex result;
result.r = c1.r + c2.r;
result.i = c1.i + c2.i;
return result;
}
ostream ostream::operator<<(ostream out, Complex number)
{
out << number.r << "+" << number.i << endl;
}
int main() {
Complex y, z;
Complex sum;
y = Complex(2, 4);
z = Complex(3, 0);
cout << "y is: " << y << endl;
cout << "z is: " << z << endl;
sum = y+z;
cout << "The sum (y+z) is: " << sum << endl;
return 0;
}