Complex numbers operator help

I'm completely stumped as to why I can't get the ostream& to run correctly.. I think it may be the simple math I did in the main function but I'm fairly certain it should be okay because of all the operator functions correct? Also for the first return where I have the (-1) before z.getImag, I'm not sure if I can do that or not... Any help would be greatly appreciated!

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

class Complex {
	double real, imag;
public:
	Complex();
	Complex(double r, double i);
	void setVals(double r, double i);
	double getReal(); // Return real part
	double getImag(); // Return imaginary part
	//double getMag(); // Return magnitude
	Complex operator+(Complex c); // Add two complex numbers
	Complex operator-(Complex c); // Subtract two complex numbers
	Complex operator*(Complex c); // Multiply two complex numbers
	Complex operator/(Complex c); // Divide two complex numbers
};
ostream& operator<<(ostream& out, Complex z) { // Write z to the output stream out
	if (z.getImag < 0) {

	return out << z.getReal << " - " << (-1)z.getImag << "i " << cout <<endl;
	}
	if (z.getImag > 0) {
		return out << z.getReal << " - " << z.getImag << "i " << cout << endl;
	}
};

//Complex operator/(double x, Complex z); // Return reciprocal of z multiplied by scalar x

double Complex::getImag() {
	return imag;
}

double Complex::getReal() {
	return real;
}

Complex::Complex() {
	real = 0;
	imag = 0;
}

Complex::Complex(double r, double i) {
	real = r;
	imag = i;
}

void Complex::setVals(double r, double i) {
	real = r;
	imag = i;
}

Complex Complex::operator*(Complex c) {
	Complex total;
	total.real = real * c.real - imag * c.imag;
	total.imag = imag * c.real + real * c.imag;

	return total;
}

Complex Complex::operator/(Complex c) {
	Complex total;
	total.real = (c.real) / (c.real * c.real + c.imag * c.imag);

	total.imag = -(c.imag) / (c.real * c.real + c.imag * c.imag);

	total = total * (*this);

	return total;
}



Complex Complex::operator-(Complex c) {
	Complex total;
	total.real = real - c.real;
	total.imag = imag - c.imag;

	return total;
}

Complex Complex::operator+(Complex c) {
	Complex total;
	total.real = real + c.real;
	total.imag = imag + c.imag;
	return total;
}

int main() {
	Complex z1;
	Complex z2;
	Complex z;

	z1.setVals(1.4, 2.1);
	z2.setVals(-3.2, 4.6);

	z = (z1 * z2) + (z1 / (z2 - z1));

	cout << z << endl;
	return 0;
}
1
2
3
4
5
6
7
8
ostream& operator<<(ostream& out, Complex z) { // Write z to the output stream out
	if (z.getImag < 0) {

	return out << z.getReal << " - " << (-1)z.getImag << "i " 
	if (z.getImag > 0) {
		return out << z.getReal << " - " << z.getImag << "i " << cout << endl;
	}
};


A few issues:

(-1)z.getImag
1. Your suspicion is warranted, this just simply isn't valid C++ syntax.

2. What happens when z.getImag == 0?

3.
<< cout <<endl; }
What is this guy doing here?

4. getImage is a function. It needs to be called with parentheses.

5. Don't put a endl or any newlines in there, that's normally up to the caller.
Just like how calling
1
2
int i;
cout << i;

doesn't automatically insert a newline.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ostream& operator<<(ostream& out, Complex z) { // Write z to the output stream out

	if (z.getImag() < 0.0)
	{
		return out << z.getReal() << " - " << -1.0 * z.getImag() << "i";
	}
	else if (z.getImag() > 0.0)
	{
		return out << z.getReal() << " + " << z.getImag() << "i";
	}
	else
	{
		return out << z.getReal(); // up to you if you want to explicitly say 0i
	}
}

Last edited on
Ohhhhh Wooooow... I can't believe I didnt notice I was missing () on the functions. Thank you so much!
Topic archived. No new replies allowed.