Reputation: 25
I have a weird error when I try to compile this piece of code.
I will explain my problem.
I defined a vector2D as following:
typedef struct LX_Vector2D
{
float vx;
float vy;
LX_Vector2D& operator =(const LX_Vector2D v); // Defined
} LX_Vector2D;
I also defined two operators on this vector:
bool operator ==(LX_Vector2D& u,LX_Vector2D& v); // Are u and v equal?
LX_Vector2D operator -(LX_Vector2D& u); // Get the opposite vector
All of these overloaded operators was defined.
So I tested these operators in the following code:
LX_Vector2D u = {3.14,-2.56};
LX_Vector2D expected_vec = {-u.vx,-u.vy};
if(expected_vec == (-u)) // ERROR
cout << "OK" << endl;
else
cout << "NO" << endl;
When I compile this code, I have this error:
no match for ‘operator==’ in ‘expected_vec == operator-((* & u))‘
I have no problem with '=' and '==' because I defined and tested them before I implemented '-'.
But when I modify this code to get this:
u = -u;
if(expected_vec == u) // OK
I have no error. I do not understand that, because it seems these two pieces of code are semantically identical.
Here is the definition of operator '-':
LX_Vector2D operator -(LX_Vector2D& u)
{
return {-u.vx,-u.vy};
}
So my question is:
Why doesn't my compiler recognize ‘expected_vec == (-u)‘ as a call of operator '==' with expected_vec and (-u) as parameters?
Another question:
How can I have the possibility to use if(expected_vec == (-u)) without any problem, if it is possible?
I use g++ 4.6.1.
Upvotes: 2
Views: 64
Reputation: 1775
in addition to the other answers, your assignment operator should also take a const&
as in:
LX_Vector2D& operator =(const LX_Vector2D& v)
note the &
after the parameter type.
As a general rule, and to avoid the construction of unnecessary copies of objects, parameters of complex types should almost always be const &
if you are not planning on changing the parameter instance. Or if you are planning on changing the parameter instance, then simply as a reference, i.e., &
.
Upvotes: 0
Reputation: 4873
Operator -
returns a temporary object:
LX_Vector2D operator -(LX_Vector2D& u)
while your comparison operator accepts a non-const
reference:
bool operator ==(LX_Vector2D& u,LX_Vector2D& v)
Temporary objects, like returned by the -
operator, cannot be used as non-const
references. It's not allowed because there is no purpose modifying an object that is about to go out of scope anyway, so the compiler makes sure you don't even attempt it.
As a general rule, you should make any function that does not modify its arguments take const
references instead, especially comparison functions:
bool operator ==(const LX_Vector2D& u,const LX_Vector2D& v)
Upvotes: 3
Reputation: 409452
The problem here is that the result from operator-
when used as part of another expression is a temporary value, and that operator==
takes non-constant references. A non-constant reference can't bind to a temporary value.
The simple solution? Make the operator==
function take constant references:
bool operator ==(const LX_Vector2D& u, const LX_Vector2D& v)
// ^^^^^ ^^^^^
// Note the use of `const`
As a general recommendation, when declaring functions that will not modify their arguments, always pass the arguments as constant. It will avoid problems like this, and may also help the compiler with possible optimizations.
Upvotes: 4