friend operator function
Dec 2, 2012 at 11:56am UTC
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;
}
Dec 2, 2012 at 11:58am UTC
Also I have a question... :)
What is "friend" attribute? And usage?
Dec 2, 2012 at 12:42pm UTC
Does noone know about this?
Dec 2, 2012 at 12:44pm UTC
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.
Dec 2, 2012 at 1:03pm UTC
If I want to write it with friend How Should I write?
In this program ,I think friend function is true.
Dec 2, 2012 at 1:39pm UTC
I had not read your talking well .It is working now.
Tanx
Dec 2, 2012 at 2:11pm UTC
You're welcome. Glad you got it. Sorry if my answer was unclear.
Topic archived. No new replies allowed.