Overloaded preincrement operator not working

I have overloaded postincrement (a++) operator in my code and I am attempting to overload the preincrement operator (++a) in my code to perform a function that squares the complex number "a", however, only the postincrement operator is working at the moment. I should make explicitly clear that both the pre and post increment operators call the same function -- they should both use the square method/function. So they should give me the same answer despite being in post/pre positions.

When called, the preincrement operator has a red line underneath it that says: "more than one operator ++ matches these operands. Yet I've explicitly delcared "++()" and "++(int)" to account for both ++a and a++.

Am I missing something? Here is my code.

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
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
#include<iomanip>
using namespace std;

class ComplexNum
{
public:
	ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
	ComplexNum& getComplexNum(); //get real and imaginary numbers from keyboard
	ComplexNum& sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
	ComplexNum& diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
	ComplexNum& prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
	ComplexNum& square(ComplexNum a); //method to find square using pre/post increment operators

	//overloaded operators
	ComplexNum& operator =  (const ComplexNum& that) = default;
	ComplexNum& operator += (const ComplexNum& that) { return sum(*this, that); }
	ComplexNum& operator -= (const ComplexNum& that) { return diff(*this, that); }
	ComplexNum& operator *= (const ComplexNum& that) { return prod(*this, that); }
	ComplexNum& operator ++() { return square(*this); } //called for ++num
	ComplexNum operator ++(int) { return square(*this); } //called for num++

	ostream& print(ostream& stm = cout) const;


private:
	float real; //float data member for real number (to be entered in by user)
	float imaginary; //float data member for imaginary number (to be entered in by user)

	//non-member overloaded operators
	//a is passed by value
	friend ComplexNum operator+ (ComplexNum a, const ComplexNum& b) { return a += b; }
	friend ComplexNum operator- (ComplexNum a, const ComplexNum& b) { return a -= b; }
	friend ComplexNum operator* (ComplexNum a, const ComplexNum& b) { return a *= b; }
	friend ComplexNum operator++(ComplexNum a) { return a++; }
	friend ComplexNum operator++(ComplexNum a) { return ++a; }
	
	friend ostream& operator<< (ostream& stm, const ComplexNum& c) { return c.print(stm); }
};

ComplexNum::ComplexNum(float a, float b)
{
	real = a;
	imaginary = b;
}

ComplexNum& ComplexNum::getComplexNum()
{
	ComplexNum keyboard;
	cout << "Enter real part of complex number: ";
	cin >> real;

	cout << "Enter imaginary part of complex number: ";
	cin >> imaginary;

	return keyboard; 
}

ComplexNum& ComplexNum::square(ComplexNum a)
{
	this->real = (a.real * a.real) - (a.imaginary * a.imaginary);
	this->imaginary = (2 * (a.real * a.imaginary));
	return *this;
}

ComplexNum& ComplexNum::sum(ComplexNum a, ComplexNum b)
{
	this->real = a.real + b.real;
	this->imaginary = a.imaginary + b.imaginary;
	return *this;
}

ComplexNum& ComplexNum::diff(ComplexNum a, ComplexNum b)
{
	this->real = a.real - b.real;
	this->imaginary = a.imaginary - b.imaginary;
	return *this;
}

ComplexNum& ComplexNum::prod(ComplexNum a, ComplexNum b)
{
	this->real = (a.real * b.real) - (a.imaginary * b.imaginary);
	this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);
	return *this;
}

ostream& ComplexNum::print(ostream& stm) const
{
	return stm << "(" << noshowpos << real << showpos << imaginary << "i)";
}

int main()
{
	ComplexNum a, b;
	cout << "First Complex Number:" << endl;
	a.getComplexNum();
	cout << endl;
	cout << "Second Complex Number:" << endl;
	b.getComplexNum();
	cout << endl;
	cout << fixed << setprecision(2)
		<< "a == " << a << '\n'
		<< "b == " << b << '\n'
		<< "a+b == " << a + b << '\n'
		<< "a-b == " << a - b << '\n'
		<< "a*b == " << a*b << '\n'
		<< "a*a == " << a*a << '\n'
		<< "b*b == " << b*b << '\n';
		cout << "a*a (using postincrement) ==" << a++ << '\n'; //works fine
		cout << "a*a (using preincrement) ==" << ++a << '\n'; //gives me an error...why?
		cout << endl;

	system("PAUSE");
}
Last edited on
> I am attempting to overload the preincrement operator (++a) in my code to
> perform a function that squares the complex number "a"
bad idea.
use ++ for next, for increment by one.

> more than one operator ++ matches these operands.
keep reading, it should tell which are those matches.

in lines 35--36 you've got
1
2
	friend ComplexNum operator++(ComplexNum a) { return a++; }
	friend ComplexNum operator++(ComplexNum a) { return ++a; }
¿what's their purpose?
¿why do they have the exact same prototype?
So I deleted the friends (didn't need them...no purpose as you rightly said!) , and corrected the overloaded operators, however, now it correctly squares when used in a++, but instead of also giving me the same answer for ++a, it squares the square of a. So a++ is say, (3)^2, and ++a is (9)^2...when they should both be (3)^2...why is this? Thanks!
the ++ operator modify its argument.
both postfix and prefix modify it, it's just that postfix should return the previous value.

1
2
3
4
5
ComplexNum operator ++(int) { //called for num++
   ComplexNum foo(*this);
   square(*this);
   return foo;
}
That is what I feared. I had the whole concept of post/pre increment wrong. Thank you ne555 for clarifying that. Best.

- elsa
Topic archived. No new replies allowed.