I'm trying to write a program that uses overloading of operators to perform operations (add, multiply, etc.). I've got the program done, but I'm having trouble modifying the driver I have to fit the program. The original program used a print method, and the new program uses overloading input and output operators. I'm having trouble using the overloaded output and input operators. I know that I have to use them instead of using the print() function, but I'm not sure how to do that.

This is the complex.cpp file:

#include "complex.h"
#include <iostream> 
using namespace std;
//using std::ostream;
//using std::istream;
// Constructor
Complex::Complex( double realPart, double imaginaryPart )
{
   real = realPart;
   imaginary = imaginaryPart;
} // end class Complex constructor
// addition operator
Complex Complex::operator+( const Complex &operand2 ) const
{
   return Complex( real + operand2.real, 
      imaginary + operand2.imaginary );
} // end function operator+
// subtraction operator
Complex Complex::operator-( const Complex &operand2 ) const
{
   return Complex( real - operand2.real, 
      imaginary - operand2.imaginary );
} // end function operator-
// multiplication operator
Complex Complex::operator*( const Complex &operand2 ) const
{
   return Complex( real * operand2.real, 
      imaginary * operand2.imaginary );
} // end function operator*
bool Complex::operator==( const Complex &operand2 ) const
{
   if((real == operand2.real) && (imaginary == operand2.imaginary))
   {
      return true;
   }
   else
   { 
      return false;
   }
} // end function operator==
bool Complex::operator!=( const Complex &operand2 ) const
{
   if((real != operand2.real) && (imaginary != operand2.imaginary))
   {
      return true;
   }
   else
   { 
      return false;
   }
} // end function operator!=
// Overloaded << operator
ostream& operator<<( ostream &output, const Complex &complex )
{
   output << complex.real << " + " << complex.imaginary << 'i';
   return output;
} // end function operator<<
// Overloaded >> operator
istream& operator>>( istream &input, Complex &complex )
{
   input >> complex.real;
   input.ignore( 3 );       // skip spaces and +
   input >> complex.imaginary;
   input.ignore( 2 );
   return input;
} // end function operator>>

This is the complex.h file:

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream> 
using std::ostream;
using std::istream;
// class Complex definition
class Complex {
   friend ostream &operator<<( ostream &, const Complex & );
   friend istream &operator>>( istream &, Complex & );
public:
   Complex( double = 0.0, double = 0.0 );     // constructor
   Complex operator+( const Complex& ) const; // addition
   Complex operator-( const Complex& ) const; // subtraction
   Complex operator*( const Complex& ) const; // multiplication
   Complex& operator=( const Complex& );      // assignment
   bool operator==( const Complex& ) const;
   bool operator!=( const Complex& ) const;
private:
   double real;       // real part
   double imaginary;  // imaginary part
}; // end class Complex
#endif // COMPLEX_H

This is the original driver:

#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h"
int main()
{
   Complex x;
   Complex y( 4.3, 8.2 );
   Complex z( 3.3, 1.1 );
   cout << "x: ";
   x.print();
   cout << "\ny: ";
   y.print();
   cout << "\nz: ";
   z.print();
   x = y + z;
   cout << "\n\nx = y + z:" << endl;
   x.print();
   cout << " = ";
   y.print();
   cout << " + ";
   z.print();
   x = y - z;
   cout << "\n\nx = y - z:" << endl;
   x.print();
   cout << " = ";
   y.print();
   cout << " - ";
   z.print();
   cout << endl;
   return 0;
} // end main

Thanks in advance for your help!

you have already coded all the required overload operators. In main() just use them like this

int main()
{

   Complex x;
   Complex y( 4.3, 8.2 );
   Complex z( 3.3, 1.1 );
   cout << "x: " << x;

you have already coded all the required overload operators. In main() just use them like this

int main()
{

    Complex x;
    Complex y( 4.3, 8.2 );
    Complex z( 3.3, 1.1 );
    cout << "x: "[COLOR=red] << x;[/COLOR]

End of quote

I tried to do what you said, but I'm still having a problem. I keep getting an error message

[Linker error] undefined reference to `Complex::Complex(double, double)' 

I think the problem is in this section:

Complex x;
Complex y( 4.3, 8.2 );
Complex z( 3.3, 1.1 );

Edited 3 Years Ago by Reverend Jim: Fixed formatting

The problem is that you're calling

Complex x;

When you haven't defined or implemented a default constructor for Complex . Add one, and the errors should be gone.

The problem is that you're calling

Complex x;

When you haven't defined or implemented a default constructor for Complex . Add one, and the errors should be gone.

How exactly would I do that? I thought I implmented the Complex objet in the complex.h file with the line "Complex complex;" Thank you so much for your help!

How exactly would I do that? I thought that i was implementing the default constructor in the complex.h file with this line of code:

Complex( double = 0.0, double = 0.0 );     // constructor

I created a complex object in the complexdriver.cpp file,

Complex complex; // create a complex object

but I'm still getting the same error.

Naa you aint defining the default constructor, you are just declaring it in Complex.h but its defination doesnt occur in Complex.cpp, thats the whole root of the problem.

Implement one in complex.cpp and all should work out fine.

Complex( double = 0.0, double = 0.0 ); // declaration of constructor taking two doubles as arguments, each argument defaulting to 0.0 if no parameter passed in.

Complex() : real(0.0), imaginary(0.0) {} //declaration and definition of default constructor setting real and imaginary data members to 0.0. No arguments can be passed to constructor.

If you don't declare any constructors the compiler will develop a default constructor and a copy constructor for you. If you explicitly declare any constructor, then you should declare at least your own default constructor explicitly, and I would recommend you declare the copy constructor explicitly, as well.

This question has already been answered. Start a new discussion instead.