error: cannot bind non-const lvalue reference of type ... to an rvalue of type ... when initializing an SimpleActionClient obj in .h file

asked 2022-01-19 03:20:20 -0500

zhixin gravatar image

updated 2022-01-19 03:22:36 -0500

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?

edit retag flag offensive close merge delete

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 failure

zhixin gravatar image zhixin  ( 2022-01-19 05:38:40 -0500 )edit