Operator overloading: Why this error.

Remove the error please.
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
#include <iostream>
using namespace std;

class b{
	int x;
	friend void setX(int);
	friend int getX();
public:
	friend void setX(int);
};

class a{
	int x;
public:
	friend void setX(int);
	friend int getX();
	int operator+(b &obj);
};

int main(){
	a obj_a;
	b obj_b;
	setX(5);
	setX(10);
	cout << "obj_a = " << getX() << endl;
	cout << "obj_b = " << getX() << endl;
	cout << "SUM of objects of different classes: " << obj_a + obj_b << endl;
	return 0;
}

int a::operator+(b &obj){
	return x + obj.x;
}
//This function serves for both classes, even it's not a member function of any class.
void setX(int value){
	x = value;//Will generate an error.
}
int getX(){
	return x;
}
//This function serves for both classes, even it's not a member function of any class
No it doesn't, and no it can't.
Topic archived. No new replies allowed.