Reputation: 2439
I have made a function like this:
void function(Objectx &x);
And I call the function like this:
Objectx o;
function(o);
in the same class.
When I compile it I get this:
error: no matching function for call to ‘function(Objectx)’
note: candidate is: void function (Objectx&)
Sorry if is a lame question, but I didn't find a solution anywhere. Do you have any suggestions?
Upvotes: 0
Views: 413
Reputation: 283674
I don't think you've shown the real code causing the error. (For one thing, the alleged code has Obectx
and the error says Objectx
)
That error would occur if you passed a temporary value (rvalue), because an non-const
reference cannot bind to an rvalue.
If the function doesn't change its parameter, change the signature to:
void function(const Objectx &x);
If the function does change its parameter, you will need to store the temporary value to a variable, and pass the variable. That way any changes made by the function end up in a variable you can access after the call.
Upvotes: 3