I implement complex class with overloaded operators. It gave me some errors after I compiled, I don't get how I solve them.The errors are generally about that "complex.cpp" does not recognize the Complex type, even there is no error on "complex.h".
complex.h
/*Definition of Complex Class. This class contains overloading operators.*/
#ifndef COMPLEX_H
#define COMPLEX_H
using std::ostream;
using std::istream;
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
const Complex &operator=(const Complex &);//assignment
bool const &operator==(const Complex &) const;//equivalent
bool const &operator!=(const Complex &) const;//not equivalent
private:
double real;//real part
double imaginary;//imaginary part
};
#endif
complex.cpp:
//Definition of Member Functions of the Complex Class
#include <iostream>
using std::cout;
using std::ostream;
using std::istream;
#include "complex.h"
//Constructor
Complex::Complex(double r, double i)
:real(r), imaginary(i){};
//Overloaded addition operator
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary +operand2.imaginary);
};
//Overloaded subtraction operator
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);
};
//Overloaded assignment operator
const Complex& Complex::operator=(const Complex &right)
{
real = right.real;
imaginary = right.imaginary;
return *this;
};
//Overloaded multiplication operator
Complex Complex::operator*(const Complex &operand2) const{
return((real *operand2.real)-(real*operand2.imaginary), (real*operand2.imaginary)-(imaginary*operand2.real));
}
bool Complex& Complex::operator==(const Complex &right) const{
if ((real == right.real) && (imaginary == right.imaginary))
return true;
else
return false;
}
bool Complex& Complex::operator!=(const Complex &right) const{
if ((real != right.real) && (imaginary != right.imaginary))
return true;
else
return false;
}
ostream &operator<<(ostream&output, const Complex &complex){//Print as Complex object as (a,b) with overloaded version
output << '(' << complex.real << "," << complex.imaginary << ')';//it allows usage as cout<<a<<b<<c
return output;
};
istream &operator>>(istream&input, Complex &complex){//Get input from the user.
input.ignore();//ignore '('
input >> complex.real;
input.ignore();//ignore ","
input >> complex.imaginary;
input.ignore();//ignore ')'
return input;//it allows usage as cin>>a>>b>>c
}
Error Lists:
1- syntax error : identifier 'Complex' 62 1
2- error C2065:'complex' : undeclared identifier 58 1
3- error C2065: 'complex' : undeclared identifier 65 1
4- error C2065: 'complex' : undeclared identifier 67 1
5- error C2086: 'bool Complex' : redefinition 48 1
6- error C2143: syntax error : missing ',' before '&' 56 1
7- error C2143: syntax error : missing ';' before '&' 40 1 8- error C2143: syntax error : missing ';' before '&' 48 1
9- error C2228: left of '.imaginary' must have class/struct/union 58 1
10- error C2228: left of '.imaginary' must have class/struct/union 67 1
11- error C2228: left of '.real' must have class/struct/union 58 1
12- error C2228: left of '.real' must have class/struct/union 65 1
13- error C2373: 'Complex::operator !=' :redefinition; different type modifiers 48 1
14- error C2373: 'Complex::operator ==' : redefinition; different type modifiers 40 1
15- error C2556: 'int &Complex::operator !=(const Complex &) const' : overloaded function differs only by return type from 'const bool &Complex::operator !=(const Complex &) const' 48 1
16- error C2556: 'int &Complex::operator ==(const Complex &) const' : overloaded function differs only by return type from 'const bool &Complex::operator ==(const Complex &) const' 40 1
17- error C2805: binary 'operator >>' has too few parameters 62 1
18- error C4430: missing type specifier -int assumed. Note: C++ does not support default-int 40 1
19-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 48 1
20-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 56 1