friend operator function

I want to write it with friend operator function.this program gives two errors.what's the problem?

1
2
Error   1   error C2678: binary '+' : no operator found which takes a left-hand operand of type 'int' (or there is no acceptable conversion)
IntelliSense: no operator "+" matches these operands  

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
#include <iostream>
#include <string>
using namespace std;
class rect
{
	int x,s;
public:
	void input();
	int calc();
	friend int operator + (rect ob,int j);
};
void rect::input()
{
	cout<<"Side:";
	cin>>x;
}
int rect::calc()
{
	return s=x*x;
}

int main()
{
	int f,t;
	cout<<"Enter Area:";
	cin>>f;
	rect obj;
	obj.input();
	obj.calc();
	t=f+obj;
	cout<<t;
	cin.get();
	cin.get();
}

int operator + (rect ob,int j)
{
	return ob.s+j;
}
Also I have a question... :)
What is "friend" attribute? And usage?
Does noone know about this?
closed account (D80DSL3A)
Try reversing function args lines 10 and 36. You seek int+rect not rect+int (which you could do with a member function).

@JM: The function (operator+) must be a friend of the rect class so it can refer to the private member s on line 38.
If I want to write it with friend How Should I write?
In this program ,I think friend function is true.
I had not read your talking well .It is working now.
Tanx
closed account (D80DSL3A)
You're welcome. Glad you got it. Sorry if my answer was unclear.
Topic archived. No new replies allowed.