challenging problem.

I tried so many times to find out what is going on with my code I found the problems in cin in main function.
and I found the others two in operator >> for overloading. Can you please help me for this issue it takes a long time from me. I would highly appreciate it.

This is head file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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 constructor
   complex operator+( const complex & ) const;
   complex operator-( const complex & ) const;
   complex operator*( const complex &) const;
   bool operator==( const complex & ) const;
   bool operator!=( const complex & ) const;
   void printcomplex()const;  // print complex number format
   
private:
   double realPart;
   double imaginaryPart;
}; // end class Complex 

#endif


This is main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

#include "complex.h"
//#include "definition.cpp"
using namespace std;

int main()
{
	complex A;
	complex B(1,2);
	complex C(1,2);
	cout << "\n\nA : ";
	A.printcomplex();
	cout << "\n\nB : ";
	B.printcomplex();
	cout << "\n\nC : ";
	C.printcomplex();

	A = B+C;
	cout << "\n\nA = B + C \n  ";
	A.printcomplex();
	cout <<" = ";
	B.printcomplex();
	cout << " +";
	C.printcomplex();
	cout << " \n\n Enter the realPart and imaginaryPart for the complex number \n\n";
	cin >> A;
	cout << "\n The complex number is : ";
	cout << A;

	A = B * C;
	cout << "\n\nA = B * C \n  ";
	A.printcomplex();
	cout <<" = ";
	B.printcomplex();
	cout << " *";
	C.printcomplex();

	if(B ==C)
		cout << "\n B and C are equal";
	else 
		cout << "\n B and C are not equal";

	if(B != A)
			cout << "\n B and A are not equal";
	else 
		cout << "\n B and A are equal";


	return 0;
}


This is cpp file. This file the error in this statement Enter >> num2.realPart >> num2.imaginaryPart; which say that realpart is not accessible because it is private


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream> 
#include "Complex.h"

using namespace std;

complex::complex( double real, double imaginary )

   :realPart(real), imaginaryPart(imaginary )
{
}// end Complex constructor
complex complex::operator+(const complex & num2)const 
{
	return complex(realPart + num2.realPart, imaginaryPart+num2.imaginaryPart);
}
complex complex::operator-(const complex & num2)const 
{
	return complex(realPart - num2.realPart, imaginaryPart+num2.imaginaryPart);
}
void complex::printcomplex()const
{
   cout << '(' << realPart << ", " << imaginaryPart << ')';
} 
complex complex::operator*(const complex & num2)const 
{
	return complex(realPart*num2.realPart-imaginaryPart*num2.imaginaryPart,
		realPart*num2.imaginaryPart+imaginaryPart*num2.realPart);
}

istream &operator>>(istream & Enter, complex &num2)
{
	Enter >> num2.realPart >> num2.imaginaryPart;
	//Enter >>  num2.realPart >>  num2.imaginaryPart;
	return Enter;

}
ostream &operator<<(ostream & printout, const complex &num2)
{
	return printout<<"(" << num2.realPart << "," << num2.imaginaryPart<<")";

}
bool complex::operator==(const complex & num2)const 
{
	if((realPart == num2.realPart)&&(imaginaryPart==num2.imaginaryPart))
		return true;
	else
		return false;
}
bool complex::operator!=(const complex & num2)const 
{
	if((realPart != num2.realPart)||(imaginaryPart!=num2.imaginaryPart))
		return true;
	else
		return false;
}

closed account (Dy7SLyTq)
shouldnt line 9 of the first file be friend istream &operator>>(istream &, complex &);
thank you so much. wow. it is a very ...... mistake.

anyway I should be careful next time
Topic archived. No new replies allowed.