Passing ros message field argument as reference
Hi all,
Here is a little code illustrating my issue :
void test2(bool &test)
{
return;
}
void test(std_msgs::Bool &bool_test)
{
//TEST 1
bool a_bool;
test2(a_bool); //Ok
//TEST 2
a_bool = bool_test.data;
test2(a_bool); //Ok
//TEST 3
bool_test.data = a_bool;
test2(bool_test.data); //Not okay
return;
}
I'm not able to compile something like this and I don't know how I can use a function taking as argument a reference of the same type of one field of a ros message. The error is the following :
error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’
test2(odom_test.data);
I'm a little bit confused about this rvalue
in the error. I've looked into it and the definition is :
rvalue is an expression that can only appear on the right hand side of an assignment
Why does bool_test
is considered as rvalue
? If it was true I wouldn't be able to write bool_test.data = a_bool;
in TEST 3
.
I know that I could do like TEST 2
and create a variable for each field of my message but I would like to understand why the TEST 3
isn't compiling. Is it even possible ?
NB : I used std_msgs::Bool
and a function with bool
argument just as an example, it happens with other type messages too.
I've deleted my answer as I misunderstood the issue. Seems like it might not be possible with std_msgs/Bool, but I'm not sure why. The issue can be made more succinctly: