Click to See Complete Forum and Search --> : problem with operator overloading!


pelegk
May 1st, 2003, 05:43 AM
i wanted to do this :
class vector3d {
private :
int x,y,z;
public :
int operator==(vector3d,vector3d);
friend int operator==(vector3d,float);
friend int operator==(float,vector3d);
};
after this declareration i have made the 3 functions,
but when i try to do :

vector3d v1,v2;
float f1=10;
cout << f1==v2;
i get an error!
why?
thnaks in advance
peleg

Graham
May 1st, 2003, 06:01 AM
operator== should return bool, not int.

Only the variant with float as the left-hand operand needs to be declared as a free function, the other two can be normal member functions.

Binary operators, when implemented as member functions, take one argument.

class vector3d
{
private :
int x,y,z;
public :
bool operator==(const vector3d&) const;
bool operator==(float) const;
};

bool operator==(float f, const vector3d& v)
{
return v == f;
}