<< overloading

I'm writing a program for complex number computations and trying to overload the ++ and <<. I'm having a problem and I really don't know if it is with << or ++. My code is:

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
//prefix increment
CComplex& CComplex::operator++()
{
	double phasor;
	phasor = atan(m_imaginary / m_real);

	m_real = m_real + cos(phasor);
	m_imaginary = m_imaginary + sin(phasor);

	return *this;
}

//postfix increment
CComplex CComplex::operator++(int x = 0)
{
	CComplex temp = *this;
	//incrementing the object
	++*this;
	//returning the old value
	return temp;
}

ostream& operator<<(ostream& out, const CComplex& obj)
{
	if (obj.m_imaginary > 0)
	{
		out << obj.m_real << " + i" << obj.m_imaginary;
	}
	if (obj.m_imaginary < 0)
	{	float temp = -obj.m_imaginary;
		out << obj.m_real << " - i" << temp;
	}
	if (obj.m_imaginary == 0)
	{
		out << obj.m_real;
	}
	return out;
}


if i do the following in my main i get the proper output:
1
2
3
4
5
6
7
8
9
10
++c3;
	++c2;
	c3.print();
	c2.set(3, -4);
	c4.set(6, 0);
	cout << c4 << endl;
	++c4;
	cout << "Value: " << c4 << endl;
	cout << " After c++ " << c4++ << endl;
	cout << " now doing ++C " << ++c4	<< endl;


however if i output all in one line it gets all messed up:

1
2
3
4
5
6
7
8
++c3;
	++c2;
	c3.print();
	c2.set(3, -4);
	c4.set(6, 0);
	cout << c4 << endl;
	++c4;
	cout << "Value: " << c4 << " After c++ " << c4++ << " now doing ++C " << ++c4	<< endl;


could anyone help?
Evaluation order doesn't have to be from left to right.
Which means it may calculate ++c4 before c4++.
The easy solution is to keep them separated.
So does the evaluation depend upon which compiler am I using? Also the values I get are something totally different. Ex if the complex number is 8+i0 then instead of getting 8 or 9 i get something like 6.xxxx +i3.xxxx. So I really cannot understand why it happens so when it works just fine with separated computations.
Please post more code, including an example where this happens.
Minor suggestion...
1
2
3
4
5
6
7
8
9
10
11
12
13
ostream& operator<<(ostream& out, const CComplex& obj)
{
	if (obj.m_imaginary > 0)
		out << obj.m_real << " + i" << obj.m_imaginary;
	else
	if (obj.m_imaginary < 0)
		out << obj.m_real << " - i" << 0 - obj.m_imaginary;
	else
	if (obj.m_imaginary == 0) // be advised, this may occur rarely
		out << obj.m_real;

	return out;
}

1
2
3
4
5
6
CComplex c10(10);
cout << c10 << endl;
cout << c10++ << endl;
cout << ++c10 << endl;

cout << " Val: " << c10 << " ++c increment " << ++c10 << " c++ increment " << c10++;


output:
10 + i0
10 + i0
12 + i0
Val: 13.1235 + i10.4988 ++c increment 13.1235 + i10.4988 c++ increment 11.5617 + i9.24939
I changed the function as suggested by you and copied all the codes in a new project and now it is behaving good. However for the line:
cout << " Val: " << c10 << " ++c increment " << ++c10 << " c++ increment " << c10++; the operations are being executed from right to left. Any suggestions how I could make it work from left to right.
http://stackoverflow.com/questions/2487608/what-is-wrong-here-associativity-evaluation-order-how-to-change-order
Any suggestions how I could make it work from left to right.

Nope, sorry.
You should just avoid modifying data while also displaying it... while also modifying it. As the link above shows, this is a stream issue, and also your overloaded operators are fine. So even if you wanted to fix the problem, it wouldn't be your problem to fix.
Hmm.. well thanks for the help!
Topic archived. No new replies allowed.