Problem with this Complex Class

Long story short, The code originally compiled without the ostream and istream. I don't understand how those two work all that well, and operators aren't my thing either.

It throws out 36 errors in between the ostream and istream.
The code is supposed to be able to read imaginary and real numbers, any tips, advice or anything to thrust me into the right direction would be greatly appreciated! Thanks CPP Forum ^^!

Header:
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

  // Fig. 11.14: Complex.h
 // Complex class definition.
//a) Modify the class to enable input and output of complex numbers via overloaded >> and
//<< operators, respectively (you should remove the print function from the class).
//b) Overload the multiplication operator to enable multiplication of two complex numbers
//as in algebra.
//c) Overload the == and != operators to allow comparisons of complex numbers.
 #ifndef COMPLEX_H
 #define COMPLEX_H

 class Complex
 {

 public:

	 void setReal(double real);
	 void setImag(double imaginary);
	 double getReal() const;
	 double getImag() const;
	 Complex( double = 0.0, double = 0.0 ); // constructor
	 Complex operator+( const Complex & ) const; // addition
	 Complex operator-( const Complex & ) const; // subtraction
	 Complex operator ==(const Complex &); //Comparison
	 Complex operator !=(const Complex &); //Negative Comparison
	 Complex operator *(const Complex &) const; // Multiplication
	 friend ostream & operator << (ostream &output, const Complex &temp);
	 friend istream & operator >> (istream &input, const Complex &temp);


 // output
 private:
 double real; // real part
 double imaginary; // imaginary part
 }; // end class Complex

#endif


CPP Functions:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Fig. 11.15: Complex.cpp
// Complex class member-function definitions.
 #include <iostream>
 #include "Complex.h" // Complex class definition
 using namespace std;

 // Constructor
 Complex::Complex( double realPart, double imaginaryPart )
 : real( realPart ),
 imaginary( imaginaryPart )
 {
 // empty body
 } // end 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 );
 }

 Complex Complex::operator!=(const Complex & operand2)
 {
	 if(real != operand2.real){
		 real = operand2.real;
	return real;
	 }
 }
 Complex Complex::operator==(const Complex & operand2) 
 {
	  if(real == operand2.real){
		 real = operand2.real;
	return real;
	 }
 }

 	 void Complex::setReal(double realPart){
		real = realPart;
	 }
	 void Complex::setImag(double imaginaryPart){
		imaginary = imaginaryPart;
	 }
	 double Complex::getReal() const{
		 return	real;}

	 double Complex::getImag() const{
		 return	imaginary;}

 // display a Complex object in the form: (a, b)
	ostream & operator << (ostream & output, const Complex &temp){
		output << "" << temp.getReal() << endl;
		output << "" << temp.getImag() << endl;
		return output;
	 }
	istream & operator >> (istream &input, const Complex &temp){
		cout << "";
		input >> temp.getImag() << endl;
		cout <<"";
		input >> temp.getReal() << endl;
	}


Main File:

1
2
3
4
5
6
7
8
9
10
11
12
13
 #include "Complex.h"
 using namespace std;

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


 system("PAUSE");
 return 0;
 }


The main used to be a print function, but our professor said to remove that part for the ostream and istream, and to be frank I barely have an idea to do it.

Again any help is greatly appreciated!
-Dreibs
> It throws out 36 errors in between the ostream and istream.
post them.


your header should be self-contained, if you want to use `ostream' then you need to #include <iostream>
Also, its name is std::ostream


1
2
3
4
5
6
7
8
Complex /*bool*/ Complex::operator==(const Complex & operand2) /*const*/
 {
   if(real == operand2.real){
      real = operand2.real; //¿ah?
      return real;
   }
  //¿else what you return?
}
Last edited on
input >> temp.getImag() << endl;

"input" is an istream &, you can't use "<<"
istream & operator >> (istream &input, const Complex &temp){
when reading input, the value of temp will be modified. const is not needed here as it will prevent such changes.
ne555 It gives 30 now after tinkering with it a bit. The errors are:

Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 13 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 18 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 25 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 23 error C2872: 'ostream' : ambiguous symbol
Error 10 error C2805: binary 'operator >>' has too few parameters
Error 20 error C2805: binary 'operator >>' has too few parameters
Error 5 error C2805: binary 'operator <<' has too few parameters
Error 15 error C2805: binary 'operator <<' has too few parameters
Error 22 error C2511: 'bool Complex::operator ==(const Complex &) const' : overloaded member function not found in 'Complex'
Error 21 error C2511: 'bool Complex::operator !=(const Complex &) const' : overloaded member function not found in 'Complex'
Error 2 error C2433: 'ostream' : 'friend' not permitted on data declarations
Error 12 error C2433: 'ostream' : 'friend' not permitted on data declarations
Error 7 error C2433: 'istream' : 'friend' not permitted on data declarations
Error 17 error C2433: 'istream' : 'friend' not permitted on data declarations
Error 29 error C2275: 'Complex' : illegal use of this type as an expression
Error 1 error C2143: syntax error : missing ';' before '&'
Error 6 error C2143: syntax error : missing ';' before '&'
Error 11 error C2143: syntax error : missing ';' before '&'
Error 16 error C2143: syntax error : missing ';' before '&'
Error 24 error C2143: syntax error : missing ';' before '&'
Error 26 error C2086: 'int ostream' : redefinition
Error 28 error C2065: 'temp' : undeclared identifier
Error 27 error C2065: 'output' : undeclared identifier
Error 4 error C2061: syntax error : identifier 'ostream'
Error 14 error C2061: syntax error : identifier 'ostream'
Error 9 error C2061: syntax error : identifier 'istream'
Error 19 error C2061: syntax error : identifier 'istream'
Error 30 error C1903: unable to recover from previous error(s); stopping compilation

For the most part I fixed what tipaye said about the input (Oops). and changed the const in the istream and ostream operators.

In a few of the errors it said that they needed more parameters, should I just add in new objects or is it something else?
Thanks in advance!
> after tinkering with it a bit.
If the errors do not correspond to the code, then they are both useless.


In `Complex.h' add #include <iostream> and change the prototypes to
1
2
	 friend std::ostream & operator << (std::ostream &output, const Complex &temp);
	 friend std::istream & operator >> (std::istream &input, Complex &temp);



input >> temp.getImag();
`.getImage()' returns a temporary, instead it should be
1
2
3
double imag;
input >> imag;
temp.setImag(imag);
Last edited on
Topic archived. No new replies allowed.