0

Homework assignment

Have to overload both the operator<<, operator== and operator!=

.h file and .cpp file is included below:

namespace JoePitz
{
 class Complex
 {
 // declare friend functions
 friend ostream &operator<<(ostream &out, const Complex &value);
 friend ostream &operator<<(ostream &out, const bool &value);
 friend istream &operator>>(istream &in, Complex &value);
 public:
 // constructor
 Complex(double real, double imaginary);
 // overloading +/-/==/!= operators
 Complex operator+(const Complex &compx2);
 Complex operator-(const Complex &compx2);
 bool operator==(const Complex &compx2);
 bool operator!=(const Complex &compx2);
 private:
 double real;
 double imaginary;
 void initialize(double real, double imaginary);
 };
 // GCC requires friend functions to be declared in name space
 ostream &operator<<(ostream &out, const Complex &value);
 ostream &operator<<(ostream &out, const bool &value);
 istream &operator>>(istream &in, Complex &value);
}
 excerpt from .cpp file
 ostream& JoePitz::operator<<(ostream &out, const Complex &value)
 {
 // print real
 cout << value.real;
 // print imaginary
 if (value.imaginary == ISZERO)
 {
 cout << POSSIGN << value.imaginary << IMAGNSGN;
 }
 else if (value.imaginary > ISZERO)
 {
 cout << POSSIGN << value.imaginary << IMAGNSGN;
 }
 else
 {
 cout << value.imaginary << IMAGNSGN;
 }
 return out;
 }
 ostream& JoePitz::operator<<(ostream &out, const bool &value)
 {
 return out;
 }
 // overloaded == operator
 bool JoePitz::Complex::operator==(const Complex &compx2)
 {
 return (this->real == compx2.real && this->imaginary == compx2.imaginary);
 }
 // overloaded != operator
 bool JoePitz::Complex::operator!=(const Complex &compx2)
 {
 return !(this->real == compx2.real && this->imaginary == compx2.imaginary);
 }

I received the following compile error:

../src/hw4.cpp:71: error: no match for 'operator<<' in 'c1 << " * * *012円"' ../src/Complex.h:54: note: candidates are: std::ostream& JoePitz::operator<<(std::ostream&, const bool&) ../src/Complex.h:53: note: std::ostream& JoePitz::operator<<(std::ostream&, const JoePitz::Complex&)

From my understanding this is a result from not knowing which overloaded function to implement.

The problem I am having is how to deal with the fact that the operator<< function return an ostream and accepts a Complex object, but the operator== function return a bool.

But I do not know how to change the operator== function to handle the bool and or the Complex object. I have attempted to add another overloaded opperator<< function that return a bool but the compiler still has problems with it.

Any assistance will be greatly appreciated.

asked May 5, 2013 at 2:54
6
  • Where is the error happening? Commented May 5, 2013 at 2:57
  • And what are the types/values of POSSIGN/IMAGNSGN? Furthermore, it's not clear why you're overloading the ostream/bool pair for operator<< (especially given that your overload does nothing). Commented May 5, 2013 at 3:04
  • Please share the code that actually produces the error. The line numbers of your error message don't correspond with the sample you posted. Even better, post an SSCCE. Commented May 5, 2013 at 3:17
  • The code that produces the error is: cout << "* * * Unit Test 3 comparison operations == != * * \n"; cout << " * * Print c1 == c1 = " << c1 == c1 << " * * *\n"; Commented May 5, 2013 at 4:49
  • const char POSSIGN = '+'; const char NEGSIGN = '-'; const char IMAGNSGN = 'i'; const double ISZERO = 0.0; Commented May 5, 2013 at 4:50

1 Answer 1

2

It's a simple operator precedence error, this

cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << c1 == c1 << " * * *\n";

should be this

cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << (c1 == c1) << " * * *\n";

<< has higher precedence than == so you need the brackets.

Remove the ostream bool overload.

Another change

 Complex operator+(const Complex &compx2);
 Complex operator-(const Complex &compx2);
 bool operator==(const Complex &compx2);
 bool operator!=(const Complex &compx2);

should all be const methods

 Complex operator+(const Complex &compx2) const;
 Complex operator-(const Complex &compx2) const;
 bool operator==(const Complex &compx2) const;
 bool operator!=(const Complex &compx2) const;
answered May 5, 2013 at 5:19
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.