what is the problem with this function?


1
2
3
4
bool & operator == (point_t & p1, point_t & p2) {
	
	return ((p1.x==p2.x) && (p1.y==p2.y))?true:false;
}


this one got problem:
error: initial value of reference to non-const must be an lvalue

if I changed it to:
1
2
3
4
bool & operator == (point_t & p1, point_t & p2) {
	bool b=((p1.x==p2.x) && (p1.y==p2.y))?true:false;
	return b;
}

then it is ok, why? I want to keep things simple.
Not sure what's wrong with the first one, but the ternary operator isn't necessary in either of your examples.
bool operator==(const point_t &p) {return (x == p.x && y == p.y);}
Neither of your implementations will be any use. In the second one, you're returning a reference to a local variable. But when the function exits, that local variable no longer exists, so the reference is garbage.

In the first case, you're attempting to return a non-const reference to something that isn't an lvalue. That's illegal.

Why are you returning a reference at all?
Topic archived. No new replies allowed.