Operator overloading

Hello guys!

I am learning operator overloading. I am kinda confused on how to define an operator.

The below code has notes to make you understand what I am trying to do.

When I run the program, I get errors:

1
2
3
4
5
6
(78): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
(67): could be 'A &A::operator =(const A &)'
(82): error C2679: binary '=' : no operator found which takes a right-hand (67): could be 'A &A::operator =(const A &)'
(87): warning C4620: no postfix form of 'operator ++' found for type 'A', using prefix form
(6) : see declaration of 'A'

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
  #include <iostream>
using namespace std;
#define NUM  10

class A
{

	int v[NUM];

public:
	A() { }
	A(int x[]) {
		for (int i = 0; i<NUM; i++)
			v[i] = x[i];
	}

	// (1) operator: [ index ], which returns v[index]

	int &operator [](int i)
	{
		return v[i];
	}


	// (2) operator:  +,  which adds the corresponding array elements

	int operator+(const A& b)
	{
		for (int i = 0; i < NUM; i++)
		{
			return v[i] = v[i] + v[i + 1];
		}
	}

	// (3) operator:  *,  which multiplies the corresponding array Elements

	int operator*(const A& b)
	{
		for (int i = 0; i < NUM; i++)
		{
			return v[i] = v[i] * v[i + 1];
		}
	}

	// (4) operator:  ++, which adds each array element by 1

	int operator++()
	{
		for (int i = 0; i < NUM; i++)
		{
			return v[i++];
		}
	}

	// (5) operator:  =, which assigns all the array elements from one object to another

/*	void operator=(const A& b)
	{
	} */


		// a print function to print out all the array elements

		void print();


};

int main()
{

	int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	A b1(x), b2, b3, b4, b5;
	cout << "b1[5]: " << b1[5] << endl;
	b2 = b1;
	cout << "b2:  ";
	b2.print();
	b3 = b1 + b2;
	cout << "b3:  ";
	b3.print();

	b4 = b1*b2;
	cout << "b4:  ";
	b4.print();

	b5 = b4;
	b5++;
	cout << "b5:  ";
	b5.print();

}
You're trying to store an int in an A object which is an invalid conversion. For lines 78 and 82, the program will first evaluate b1 + b2 and b1 * b2 respectively. These operations return an int. Then you're trying to put that int into an A object. My suggest would be to have your overload return objects themselves to overcome this problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
A operator+(const A& b)
	{
	    A return_object;
		for (int i = 0; i < NUM; i++)
		{
			return_object.v[i] = (*this).v[i] + b.v[i];
		}
		return return_object;
	}

	// (3) operator:  *,  which multiplies the corresponding array Elements

	A operator*(const A& b)
	{
	    A return_object;
		for (int i = 0; i < NUM; i++)
		{
			return_object.v[i] = (*this).v[i] * b.v[i];
		}
		return return_object;
	}


Study up on the (*this) operator! It's your friend!
Other things, you were returning as soon as you entered the loop in your original array. It never added or multiplied every array element. Only the first.

I did not touchthe ++ operator which still has an error. I'm not entirely sure what the comments are calling you to do for it.
Last edited on
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
#include <iostream>
using namespace std;
#define NUM  10

class A
{

	int v[NUM];

public:
	A() { }
	A(int x[]) {
		for (int i = 0; i<NUM; i++)
			v[i] = x[i];
	}
        // (1) operator: [ index ], which returns v[index]
	int& operator[](int i){
		static int err = -1;
		if (i < 0 || i > NUM - 1)
			return err;
		else
			return this->v[i];
	}
	
	// (2) operator:  +,  which adds the corresponding array elements

	A operator+(const A& rhs)
	{
		A temp;
		for (int i = 0; i < NUM; i++)
		{
			temp.v[i] = this->v[i] + rhs.v[i];
		}
		return temp;
	}

	// (3) operator:  *,  which multiplies the corresponding array Elements

	A operator*(const A& rhs)
	{
		A temp;
		for (int i = 0; i < NUM; i++)
		{
			temp.v[i] = this->v[i]*rhs.v[i];
		}
		return temp;
	}

	// (4) operator:  ++, which adds each array element by 1

	A& operator++() {
		for (int i = 0; i < NUM; i++)
			this->v[i] += 1;
		return *this;
	}
	A operator++(int) {
		A tmp(*this); 
		operator++(); 
		return tmp;   
	}

	// (5) operator:  =, which assigns all the array elements from one object to another

	A& operator=(const A& rhs)
	{
		if (this != &rhs){
			for (int i = 0; i < NUM; i++)
				this->v[i] = rhs.v[i];
		}
		return *this;
	} 

        //Operator << 
	friend ostream &operator<<(ostream& out, const A& thing){
		for (int i = 0; i < NUM; i++)
			out << thing.v[i] << ' ';
		return out;
	}

};

int main()
{

	int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	A b1(x), b2, b3, b4, b5;
	cout << "b1[5]: " << b1[5] << endl;
	b2 = b1;
	cout << "b1:  ";
	cout << b1;
	cout << "\nb2:  ";
	cout << b2;
	b3 = b1 + b2;
	cout << "\nb3:  ";
	cout << b3;

	b4 = b1*b2;
	cout << "\nb4:  ";
	cout << b4;

	b5 = b4;
	++b5;
	cout << "\nb5:  ";
	cout << b5 << endl;

}
Last edited on
Topic archived. No new replies allowed.