• Advertisement
Sign in to follow this  

Problem with overloaded operator

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good evening, I'm having some trouble with my overloaded operators in C++, specifically the << overloaded operation. I am doing a complex number class. Here is the code: class complex0 { public: complex0 operator+(const complex0& comp) const { return complex0(real + comp.real, imaginary + comp.imaginary); } complex0 operator-(const complex0& comp) const { return complex0(real - comp.real, imaginary - comp.imaginary); } complex0 operator*(const complex0& comp) const { return complex0(real * comp.real, imaginary * comp.imaginary); } complex0 operator*(double num) const { return complex0(real * num, imaginary * num); } complex0 operator~() { return complex0(real, imaginary * -1); } friend ostream& operator<<(ostream& os, complex0& comp); friend istream& operator>>(istream& is, complex0& comp); friend complex0 operator*(double num, complex0& comp); complex0():real(0), imaginary(0) {} complex0(double r, double i):real(r), imaginary(i) {} private: double real; double imaginary; }; complex0 operator*(double num, complex0& comp) { return(complex0(num * comp.real, num * comp.imaginary)); } ostream& operator<<(ostream& os, complex0& comp) { os<<"("<<comp.real<<","<<comp.imaginary<<"i)"; return os; } istream& operator>>(istream& is, complex0& comp) { double real; double imaginary; cout<<"real: "; is>>real; cout<<endl; cout<<"imaginary: "; is>>imaginary; comp.real = real; comp.imaginary = imaginary; return is; } #include <iostream> using namespace std; #include "complex0.h" // to avoid confusion with complex0.h int main() { complex0 a(3.0, 4.0); // initialize to (3,4i) complex0 c; cout << "Enter a complex number (q to quit):\n"; while (cin >> c) { c = c + a; cout << "c is " << c << '\n'; cout << "complex conjugate is " << ~c << '\n'; cout << "a is " << a << '\n'; cout << "a + c is " << a + c << '\n'; cout << "a - c is " << a - c << '\n'; cout << "a * c is " << a * c << '\n'; cout << "2 * c is " << 2 * c << '\n'; cout << "c * 4 is " << c * 4 << '\n'; cout << "Enter a complex number (q to quit):\n"; } cout << "Done!\n"; return 0; } I get the following error: "no match for 'operator<<' error" on line 28 of the main function, and for every other line where a mathematical operation is done in between "<<" operators. I tested the mathematical operators (for example, in the line c = c + a in int main()), and they seem to work okay. I also noticed that the problem occurs when the << operator is followed by an operation, such as << c * 4<<. What's the order of operations for how the statement is executed? I was under the impression that the c * 4 would be executed first, then the << operator. I suspect that's my problem. Can anyone offer any suggestions? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You are using non-const references in a number of places, which prevents expressions being used (you can bind a temporary to a const reference, but not a non-const one).

As an example, you operator << code should be like this:

ostream& operator<<(ostream& os, const complex0& comp)
{
os << "(" << comp.real << "," << comp.imaginary << "i)";
return os;
}


Don't forget to change the friend declaration to match.

I assume you are doing this as an excersize in operator overloading in C++. I will just point out that C++ already has a complex class, std::complex defined in <complex>.

Share this post


Link to post
Share on other sites
Thank you for your help...that solved my problem.

I'd still like some clarification, if that's okay.

I noticed that if I leave the friend function operator*, for example, as a non-const function, I have no problem with execution. Doesn't that also use a temporary reference? Of course, it is possible that I am misunderstanding what constitutes a temporary. Sorry if it's a silly question. I just want to make sure I have everything straight.

Thanks again!

Share this post


Link to post
Share on other sites
The result of the operations in your print statements are temporaries.

Lets look at the offending line:

cout << "2 * c is " << 2 * c << '\n';


To make it more explicit, this is similar to:

complex temp = 2 * c;
cout << "2 * c is " << temp << '\n';


Now, as you can see the operator<<() call is receiving the temporary value. However, the call to operator*() does not receive a temporary, it takes c. To pass it a temporary you would do something like this:

cout << "3 * (2 * c) is " << 3 * (2 * c) << '\n';


Again, more explicitly this is like:

complex temp1 = 2 * c;
complex temp2 = 3 * temp1;
cout << "3 * (2 * c) is " << temp2 << '\n';


This new snippet (not the explicit version) will no longer compile unless operator*() is modified to take a const complex reference.

Does this clarify what going on for you?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement