error: cannot bind non-const lvalue reference of type ... to an rvalue of type ... when initializing an SimpleActionClient obj in .h file
I recently developed a ros pkg, I want to initialize an action client object as a member var in head file, so that I can use it in different member functions.
Here is the code in .h file:
actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> move_planner_
= actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction>("move_base", true);
Here is actionlib::SimpleActionClient implementation:
template<class ActionSpec>
class SimpleActionClient
{
...
SimpleActionClient(const std::string & name, bool spin_thread = true)
: cur_simple_state_(SimpleGoalState::PENDING)
{
initSimpleClient(nh_, name, spin_thread);
}
...
}
But compiler give me error:
error: cannot bind non-const lvalue reference of type ‘actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction_<std::allocator<void> > >&’ to an rvalue of type ‘actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction_<std::allocator<void> > >’
= actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction>("move_base", true);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I don't understand why actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> move_planner_
turn out to be a rvalue.
Does anyone know how to initilize a action client as a member variable in head file?
Asked by zhixin on 2022-01-19 04:20:20 UTC
Comments
In head file, modifying to
actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> move_planner_ = actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction>{"move_base", true};
solve the problem. However I'm still confused by the prevoius failureAsked by zhixin on 2022-01-19 06:38:40 UTC