Overloading arithmetic operators as member funtions

Help! I was studying operator overloading these days. I was able to overload +, -, * and / for a complex number class I wrote as friend and non-member functions, but failed miserably at overloading them as member functions. The code can be seen below.

I'm using MS Visual C++ express 2012. Error codes are in the 2nd post. The C2248 error seemed to be especially peculiar: how come a class type cannot access the private data members? This is not making sense at all!

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
ComplexNumber.h
#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H

using namespace std; 

class ComplexNumber
{
public:
	ComplexNumber();
	ComplexNumber(double realPart, double imPart);
	ComplexNumber(double realPart);
	const ComplexNumber operator +(const ComplexNumber& amount);
	const ComplexNumber operator -(const ComplexNumber& amount);
	const ComplexNumber operator *(const ComplexNumber& amount);
	const ComplexNumber operator /(const ComplexNumber& amount);
	bool operator ==( const ComplexNumber& amount);

private:
	double real;
	double im;
};
#endif

ComplexNumber.cpp

#include "stdafx.h"
#include <iostream>
#include "ComplexNumber.h"

using namespace std; 

ComplexNumber::ComplexNumber()
	:real(0.0),
	im(0.0)
{}

ComplexNumber::ComplexNumber(double realPart, double imPart)
	:real(realPart),
	im(imPart)
{}

ComplexNumber::ComplexNumber(double realPart)
	: real(realPart),
	im(0.0)
{}

const ComplexNumber operator +(const ComplexNumber& amount)
{
	ComplexNumber temp;
	temp.real=real+amount.real;
	temp.im =im + amount.im;
	cout << temp.real << "+" << temp.im << "i" << endl;
	return temp;
}

const ComplexNumber operator -(const ComplexNumber& amount)
{
	ComplexNumber temp;
	temp.real=real-amount.real;
	temp.im=im-amount.im;
	if(temp.im<0)
		cout<<temp.real<<temp.im<<"i"<<endl;
	else
		cout<<temp.real<<"+"<<temp.im<<"i"<<endl;
	return temp;
}

const ComplexNumber operator *(const ComplexNumber& amount)
{
	ComplexNumber temp;
	temp.real=real*amount.real-im*amount.im;
	temp.im=im*amount.real+amount.im*real;
	if(temp.im<0)
		cout<<temp.real<<temp.im<<"i"<<endl;
	else
		cout<<temp.real<<"+"<<temp.im<<"i"<<endl;
	return temp;
}

const ComplexNumber operator /(const ComplexNumber& amount)
{
	ComplexNumber temp;
	temp.real=(real*amount.real+im*amount.im)/(amount.real*amount.real+amount.im*amount.im);
	temp.im=(im*amount.real-real*amount.im)/(amount.real*amount.real+amount.im*amount.im);
	if(temp.im<0)
		cout<<temp.real<<temp.im<<"i"<<endl;
	else
		cout<<temp.real<<"+"<<temp.im<<"i"<<endl;
	return temp;
}

bool operator ==( const ComplexNumber& amount)
{
	if((real==amount.real) && (im==amount.im))
		return true;
	else
		return false;
}

main.cpp //test driver

#include "stdafx.h"
#include <iostream>
#include "ComplexNumber.h"

using namespace std; 

int _tmain()
{
	double number1, number2, number3, number4;
	ComplexNumber C1, C2;
	ComplexNumber addition, substraction, product, quotient;

	cout<<"Please enter the first complex number(real part followed by imaginary part)"<<endl;
	cin>>number1>>number2;

	cout<<"Please enter the second complex number(real part followed by imaginary part)"<<endl;
	cin>>number3>>number4;

	C1=ComplexNumber(number1, number2);
	C2=ComplexNumber(number3, number4);

	cout<<"The sum of the two complex numbers is:\n";
	addition=C1+C2;
	cout<<"The substraction of the two complex numbers is:\n\n";
	substraction=C1-C2;
	cout<<"The product of the two complex numbers is:\n\n";
	product=C1*C2;
	cout<<"The quotient of the two complex numbers is:\n\n";
	quotient=C1/C2;

	cout<<"Testing if the two complex numbers are equal:\n\n";
	if(C1==C2)
		cout<<"The two complex number are equal."<<endl;
	else
		cout<<"The two complex number are not equal."<<endl;

	return 0;
}


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
Error	1	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	25	1	ComplexNumber
Error	2	error C2065: 'real' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	25	1	ComplexNumber
Error	3	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	26	1	ComplexNumber
Error	4	error C2065: 'im' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	26	1	ComplexNumber
Error	5	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	27	1	ComplexNumber
Error	6	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	27	1	ComplexNumber
Error	7	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	34	1	ComplexNumber
Error	8	error C2065: 'real' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	34	1	ComplexNumber
Error	9	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	35	1	ComplexNumber
Error	10	error C2065: 'im' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	35	1	ComplexNumber
Error	11	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	36	1	ComplexNumber
Error	12	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	37	1	ComplexNumber
Error	13	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	37	1	ComplexNumber
Error	14	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	39	1	ComplexNumber
Error	15	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	39	1	ComplexNumber
Error	16	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	46	1	ComplexNumber
Error	17	error C2065: 'real' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	46	1	ComplexNumber
Error	18	error C2065: 'im' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	46	1	ComplexNumber
Error	19	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	46	1	ComplexNumber
Error	20	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	47	1	ComplexNumber
Error	21	error C2065: 'im' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	47	1	ComplexNumber
Error	22	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	47	1	ComplexNumber
Error	23	error C2065: 'real' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	47	1	ComplexNumber
Error	24	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	48	1	ComplexNumber
Error	25	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	49	1	ComplexNumber
Error	26	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	49	1	ComplexNumber
Error	27	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	51	1	ComplexNumber
Error	28	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	51	1	ComplexNumber
Error	29	error C2805: binary 'operator /' has too few parameters	<dir>\complexnumber2\complexnumber\complexnumber.cpp	56	1	ComplexNumber
Error	30	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	58	1	ComplexNumber
Error	31	error C2065: 'real' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	58	1	ComplexNumber
Error	32	error C2065: 'im' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	58	1	ComplexNumber
Error	33	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	58	1	ComplexNumber
Error	34	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	59	1	ComplexNumber
Error	35	error C2065: 'im' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	59	1	ComplexNumber
Error	36	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	59	1	ComplexNumber
Error	37	error C2065: 'real' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	59	1	ComplexNumber
Error	38	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	60	1	ComplexNumber
Error	39	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	61	1	ComplexNumber
Error	40	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	61	1	ComplexNumber
Error	41	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	63	1	ComplexNumber
Error	42	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	63	1	ComplexNumber
Error	43	error C2805: binary 'operator ==' has too few parameters	<dir>\complexnumber2\complexnumber\complexnumber.cpp	68	1	ComplexNumber
Error	44	error C2065: 'real' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	69	1	ComplexNumber
Error	45	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	69	1	ComplexNumber
Error	46	error C2065: 'im' : undeclared identifier	<dir>\complexnumber2\complexnumber\complexnumber.cpp	69	1	ComplexNumber
Error	47	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	69	1	ComplexNumber
the first thing you should do is learn more about classes, and const correctness with classes.

Look up what the this pointer is, it makes it easier for you to call member functions of a class with reference to the 'current' instance of the object (it's also probably easier).

Furthermore, it is improper to declare a new instance of a class within the class when modifying the class. Operators are mean to manipulate an instance of a class, not another instance.

here's an example:

class myclass{
public:

//......

myclass& operator=(const myclass& m)
{
this->member = myclass.member;
return *this;
}

myclass& operator+(const unsigned int& i)
{
this->member += i;
return *this;
}

//....

};


when you perform the following operation:

1
2
myclass c(init_var);
c = (c + 1)


it is the same as:

c.operator=(c.operator+(1));

if .operator+(const unsigned int&) doesn't return a correct value (in this case, it is its self, which makes it redundant, but for your benefit...) it will throw a slew of errors like the onse you see.

Since I am doing this correctly, I could even do somthing like this:

if((c + 1)[5] == "an element")
{
cout<< c[5]<< endl;
}
Last edited on
Mr. IWishIKnew,

I had tried this pointer.

1
2
3
4
5
6
7
8

const ComplexNumber operator +(const ComplexNumber& amount)
{
	this->real += amount.real;
	this->im += amount.im;
	return *this;
}


This is what I got:

1
2
3
4
5
6
7
Error	1	error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers	<dir>\complexnumber2\complexnumber\complexnumber.cpp	29	1	ComplexNumber
Error	2	error C2227: left of '->real' must point to class/struct/union/generic type	<dir>\complexnumber2\complexnumber\complexnumber.cpp	29	1	ComplexNumber
Error	3	error C2248: 'ComplexNumber::real' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	29	1	ComplexNumber
Error	4	error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers	<dir>\complexnumber2\complexnumber\complexnumber.cpp	30	1	ComplexNumber
Error	5	error C2227: left of '->im' must point to class/struct/union/generic type	<dir>\complexnumber2\complexnumber\complexnumber.cpp	30	1	ComplexNumber
Error	6	error C2248: 'ComplexNumber::im' : cannot access private member declared in class 'ComplexNumber'	<dir>\complexnumber2\complexnumber\complexnumber.cpp	30	1	ComplexNumber
Error	7	error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers	<dir>\complexnumber2\complexnumber\complexnumber.cpp	31	1	ComplexNumber


This drove me nuts. The function is definitely non-static!
Last edited on
remove the const at the beginning of the function dec and make it return by address.
closed account (D80DSL3A)
And resolve the scope where defining the functions:
Not ComplexNumber operator +(const ComplexNumber& amount)
but ComplexNumber ComplexNumber::operator +(const ComplexNumber& amount)
> it is improper to declare a new instance of a class within the class when modifying the class.
If you need to create another object, create it.
You do not modify classes, you modify objects.

1
2
3
4
5
 myclass& operator+(const unsigned int& i)
    {
         this->member += i;
         return *this;
    }

¡No! ¡¿How dare you to modify the object?! That's counter intuitive
c = a+b; ¿why do you think is a good idea for `a' to change?

> make it return by address.
Do not return an address to a temporary.


@OP: you are defining global functions. To define member functions do as fun2code shows.
If you want to observe const-correctness the prototype should be const ComplexNumber operator /(const ComplexNumber& amount) const;
Last edited on
ah, yes. That makes more sense. Sorry, I have been studying the Qt libraries lately, it's been mind-numbing.

you only want to have the operators self-modify for operators ++, +=, -=, etc...

Thanks ne for correcting me.
Last edited on
Thanks to all of you, sirs!
Topic archived. No new replies allowed.