In the program below I am trying to overload the +, -, *, >>, <<, ==, and != operators. I cannot get the code to compile and am not sure what I'm doing wrong. I have posted the code and error messages below:

Complex.h

#include <iostream>
using namespace std;

class Complex
{
public:
	Complex();
	Complex(double, double);
	Complex operator+(Complex);
	Complex operator-(Complex);
	Complex operator*(Complex);
	bool operator==(Complex);
	bool operator!=(Complex);
	friend ostream &operator<<(ostream &, Complex &);
	friend istream &operator>>(istream &, Complex &);
private:
	double real;
	double imaginary;
};

Complex.cpp

#include <iostream>
#include "Complex.h"

using namespace std;

Complex::Complex()
{

}

Complex::Complex( double a, double b )
{
	real = a;
	imaginary = b;
}

// addition operator
Complex Complex::operator+(Complex operand2) 
{
   return Complex( real + operand2.real,
      imaginary + operand2.imaginary);
} // end function operator+

// subtraction operator
Complex Complex::operator-( Complex operand2 ) 
{
   return Complex( real - operand2.real,
      imaginary - operand2.imaginary);
} // end function operator-

// mutilplication operator
Complex Complex::operator*( Complex operand2 ) 
{
   return Complex( real * operand2.real,
      imaginary * operand2.imaginary);
} // end function operator-

bool Complex::operator==( Complex operand2 )
{
	bool result = true;
	double first = real;
	double second = imaginary;
	
	if(real == imaginary)
	{
		result = true;
	}
	else
		result = false;
	return result;
}

bool Complex::operator!=( Complex operand2 )
{
	bool result = true;
	double first = real;
	double second = imaginary;
	
	if(real != imaginary)
	{
		result = true;
	}
	else
		result = false;
	return result;
}

ostream &operator<<(ostream &out, Complex &c)
{
	out << c.real << c.imaginary;
	return out;
}

istream &operator>>(istream &in, Complex &c)
{
	in >> c.real;
	in.ignore();
	in >> c.imaginary;
	return in;
}

Main.cpp

#include <iostream>

using std::cout;
using std::endl;

#include "Complex.h"

int main()
{
    Complex x, y, z;


    // Test creation of Complex objects
    cout << "x: ";
    cout << x;;  // REPLACE all print() calls with output (<<) statements
    cout << "\ny: ";
    cout << y;
    cout << "\nz: ";
    cout << z;
    cout << endl;

    // Test overloaded addition operator
    x = y + z;
    cout << y << "+" << z << " = " << x << endl;

    // Test overloaded subtraction operator
    x = y - z;
    cout << y << " - " << z << " = " << x << endl;

    // ADD code to test overloaded multiplication operator
	x = y * z;
    cout << y  << " * " << z << " = " << x << endl;
    // ADD code to test equality (==) operator
	cout << y << ( ( y == z ) ? " == " : " != " ) << z
         << " according to the overloaded == operator\n";

    // ADD code to test inequality (!=) operator
	cout << y << ( ( y != z ) ? " == " : " != " ) << z
         << " according to the overloaded == operator\n";

	system("pause");
    return 0;
}

Error messages:
'Complex' : 'class' type redefinition
see declaration of 'Complex'
'x' uses undefined class 'Complex'
'y' uses undefined class 'Complex'
'z' uses undefined class 'Complex'

You need header-guards.

Simply do this for your header file:

#ifndef MY_COMPLEX_HEADER
#define MY_COMPLEX_HEADER

#include <iostream>
using namespace std;

class Complex
{
public:
	Complex();
	Complex(double, double);
	Complex operator+(Complex);
	Complex operator-(Complex);
	Complex operator*(Complex);
	bool operator==(Complex);
	bool operator!=(Complex);
	friend ostream &operator<<(ostream &, Complex &);
	friend istream &operator>>(istream &, Complex &);
private:
	double real;
	double imaginary;
};

#endif

Ok Awesome that made my code compile and run so I could see that something is completley wrong. It is display really random numbers where it needs to accept input from the user and is not. The program is supposed to add, subtract, multiply and compare complex numbers in the form real + imaginary * i where i is the square root of -1. I understand the Math concept but I guess I don't understand how to write it into a program. Any advice?

You create your Complex numbers without giving them an initial value (i.e. your default constructor gives no default value to the real and imaginary parts). Generally, you should do the following for your constructors:

class Complex
{
public:
	//Complex(); //don't provide a default constructor, instead, provide default values for the parametrized constructor:
	Complex(double r = 0.0, double i = 0.0);
	Complex operator+(Complex);
	Complex operator-(Complex);
	Complex operator*(Complex);
	bool operator==(Complex);
	bool operator!=(Complex);
	friend ostream &operator<<(ostream &, Complex &);
	friend istream &operator>>(istream &, Complex &);
private:
	double real;
	double imaginary;
};

With the above (and eliminating the definition of the default constructor), you should have default constructed Complex objects that have both real and imaginary parts being 0. After that, you should provide values to your complex numbers (either hard-coded values via parameters to the constructor, or input values from the user using the cin >> operator).

That should get rid of the "display really random numbers" problem, which was caused by having no initial values for your Complex numbers.

I have edited my code and closely followed examples in my book but this is not compiling...any advice as to what may be wrong?

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
#include <string>

class Complex
{
	friend ostream &operator<<( ostream &, const Complex & );
	friend istream &operator>>( istream &, Complex & );
public:
	Complex(double = 0.0, double = 0.0);		
	Complex operator<<( const Complex & ) const;
	Complex operator >> (const Complex & ) const;
	Complex operator+( const Complex & ) const;
	Complex operator-( const Complex & ) const;
	Complex operator*( const Complex & ) const;
private:
	double real;
	double imaginary;
};

#endif

Complex.cpp

#include<iomanip>
#include <iostream>
#include "Complex.h"

using namespace std;

Complex::Complex( double realPart, double imaginaryPart )
	: real( realPart ),
	imaginary( imaginaryPart )
{

}

ostream& operator<<( ostream &output, const Complex & c )
{
	output << c.real << c.imaginary;;
	return output;
}

istream& operator>>( istream &input, Complex &c )
{
	input.ignore();
	input >> c.real;
	input >> c.imaginary;
	return input;
}
// 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-

// mutilplication operator
Complex Complex::operator*( const Complex &operand2 ) const
{
   return Complex( real * operand2.real,
      imaginary * operand2.imaginary);
} // end function operator*
#include <iostream>
using namespace std;

#include "Complex.h"

Main.cpp
int main()
{
    Complex x, y, z;

	cout << "Enter value for y: ";
	cin >> y;

	cout << "Enter value for z: ";
	cin >> z;

    // Test creation of Complex objects
    cout << "x: ";
    cout << x;
    cout << "\ny: ";
    cout << y;
    cout << "\nz: ";
    cout << z;
    cout << endl;

    // Test overloaded addition operator
    x = y + z;
    cout << "\nx = y + z:\n(";
    cout << x;
    cout << ") = (";
    cout << y;
    cout << ") + (";
    cout << z;
    cout << ")" << endl;

    // Test overloaded subtraction operator
    x = y - z;
    cout << "\nx = y - z:\n(";
    cout << x;
    cout << ") = (";
    cout << y;
    cout << ") - (";
    cout << z;
    cout << ")" << endl;

    // ADD code to test overloaded multiplication operator
	x = y * z;
    cout << "\nx = y * z:\n(";
    cout << x;
    cout << ") = (";
    cout << y;
    cout << ") * (";
    cout << z;
    cout << ")" << endl;

	system("pause");
    return 0;
}

The only thing that I can see that is wrong is the "Main.cpp" on line 6 of the last piece of code you posted.

If it still doesn't compile, please provide the error that it prints out.

Also, your multiplication operator is mathematically wrong, read up on complex numbers.

Oops I think that "Main.cpp" is from where I was trying to label it above the code tag but accidently put it inside the code. The error messages are:
syntax error : missing ';' before '&'
'ostream' : 'friend' not permitted on data declarations
missing type specifier - int assumed. Note: C++ does not support default-int
syntax error : identifier 'ostream'
missing type specifier - int assumed. Note: C++ does not support default-int
binary 'operator <<' has too few parameters
syntax error : missing ';' before '&'
'istream' : 'friend' not permitted on data declarations

and there are many many more.

I am sure my Math on multiplication is wrong I just threw something in there I figured if I can ever get the program to compile I can fix the math parts then.

I now have a code that is compiling but having some trouble getting the input. It is setting x = y and not properly doing the math. Any ideas on where I need to fix my code to make it correctly add and subtract the complex numbers?

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
using namespace std;

class Complex
{
	friend ostream &operator<<(ostream &, const Complex& );
	friend istream &operator>>(istream &, Complex & );

public:
	Complex( double = 0.0,  double = 0.0); //default constructorr

	Complex operator+(const Complex & ) const; //addition operator
	Complex operator-(const Complex & ) const; //subtratction operator
	Complex operator*(const Complex & ) const; //multiplication operator
	friend bool operator==(const Complex &, const Complex & ); //equality operator
	friend bool operator!=(const Complex &, const Complex & ); //inequality operator
private:
	double real; //real number
	double imaginary; //imaginary number
};

#endif

Complex.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "Complex.h"

using namespace std;

//default constructor for class Complex
Complex::Complex( double realPart, double imaginaryPart ) 
	:real(realPart),
	imaginary(imaginaryPart)
{

}

ostream& operator<<(ostream& output, const Complex& c)
{
	output << c.imaginary << "+" << c.real << "i";
	return output;
}

istream& operator>>(istream& input,  Complex& c)
{
	input.ignore();
	input >> c.real;	
	input >> c.imaginary;	
	return input;
}

Complex Complex::operator+( const Complex& operand2 ) const
{
	Complex( real - operand2.real,      imaginary - operand2.imaginary); 
	return Complex(real, imaginary );
}

Complex Complex::operator-( const Complex& operand2 ) const
{
	Complex( real - operand2.real,      imaginary - operand2.imaginary ); 
	return Complex(real, imaginary);
}

Complex Complex::operator*( const Complex& operand2 ) const
{
	Complex( real * operand2.real, imaginary * operand2.imaginary ); 
	return Complex(real, imaginary);
}

bool operator==( const Complex& c1, const Complex& c2 ) 
{
	if(c1.real == c2.real & c1.imaginary == c2.imaginary )
		return true;
	else
		return false;
}

bool operator!=( const Complex& c1, const Complex& c2 ) 
{
	if(c1.real != c2.real * c1.imaginary!= c2.imaginary )
		return true;
	else
		return false;
}

Main.cpp

#include <iostream>
using namespace std;

#include "Complex.h"

int main()
{
	Complex x, y, z;

	cout << "Enter value for y: ";
	cin >> y;

	cout << "Enter value for z: ";
	cin >> z;

    // Test creation of Complex objects
    cout << "\ny: ";
    cout << y;
    cout << "\nz: ";
    cout << z;
    cout << endl;

    // Test overloaded addition operator
    x = y + z;
    cout << "\nx = y + z:\n(";
    cout << x;
    cout << ") = (";
    cout << y;
    cout << ") + (";
    cout << z;
    cout << ")" << endl;

    // Test overloaded subtraction operator
    x = y - z;
    cout << "\nx = y - z:\n(";
    cout << x;
    cout << ") = (";
    cout << y;
    cout << ") - (";
    cout << z;
    cout << ")" << endl;

    // ADD code to test overloaded multiplication operator
	x = y * z;
    cout << "\nx = y * z:\n(";
    cout << x;
    cout << ") = (";
    cout << y;
    cout << ") * (";
    cout << z;
    cout << ")" << endl;

	//Code to test overloaded == operator
	if(y == z)
	cout << "true" << endl;
	else 
		cout << "false" << endl;

	//Code to test overloaded != operator
	if(y != z)
	cout << "true" << endl;

	system("pause");
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.