I am a newbie programmer and I am writing a simple program that adds two complex numbers. I have overloaded << in the following way:
ostream& operator << (ostream& output, Complex &complex_num){
output << complex_num.realPart << " + " << "(" << complex_num.imaginaryPart << ")i" <<endl;
return output;
}
My addition function is as follows:
Complex operator +(Complex &c2){
Complex temp;
temp.realPart=realPart+c2.realPart;
temp.imaginaryPart=imaginaryPart + c2.imaginaryPart;
return temp;
}
In my main function, when I try to print out the result by typing in :
cout << "ADDITION OF THE TWO COMPLEX NUMBERS: "<<num1 + num2<< endl;
I get an error saying no match for operator <<. However, when I assign another object num3 = num1 + num2 and then write the following code, the program runs fine.
cout << "ADDITION OF THE TWO COMPLEX NUMBERS: "<<num3<< endl;
What is happening here? Can anyone please help me?