ROS2 error creating a service server as a member function?
I am trying to create a service server with a callback as a member function:
class FirstNode : public rclcpp::Node
{
public:
explicit FirstNode(const std::string & node_name) : Node(node_name)
{
RCLCPP_INFO(this->get_logger(), "%s started", node_name.c_str());
server_ = this->create_service<std_srvs::srv::Trigger>("first_service", std::bind(&FirstNode::handle_service, this, _3, _2, _1));
}
private:
void handle_service(
const std::shared_ptr<rmw_request_id_t> request_header,
const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
std::shared_ptr<std_srvs::srv::Trigger::Response> response)
{
(void)request_header;
RCLCPP_INFO(this->get_logger(), "Service Triggered");
}
rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr server_;
};
However, I get the following error:
In file included from /Users/ahl/happtec_ws/src/first_node_cpp/src/first_node.cpp:15:
In file included from /Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/rclcpp.hpp:144:
In file included from /Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/executors.hpp:21:
In file included from /Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp:24:
In file included from /Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/executor.hpp:31:
In file included from /Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/node_interfaces/node_base_interface.hpp:25:
In file included from /Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/callback_group.hpp:24:
In file included from /Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/service.hpp:27:
/Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/any_service_callback.hpp:81:46: error: no viable overloaded '='
shared_ptr_with_request_header_callback_ = callback;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~
/Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/create_service.hpp:43:24: note: in instantiation of function template specialization 'rclcpp::AnyServiceCallback<std_srvs::srv::Trigger>::set<std::__1::__bind<void (FirstNode::*)(std::__1::shared_ptr<rmw_request_id_t>, std::__1::shared_ptr<std_srvs::srv::Trigger_Request_<std::__1::allocator<void> > >, std::__1::shared_ptr<std_srvs::srv::Trigger_Response_<std::__1::allocator<void> > >), FirstNode *, const std::__1::placeholders::__ph<3> &, const std::__1::placeholders::__ph<2> &, const std::__1::placeholders::__ph<1> &>, nullptr>' requested here
any_service_callback.set(std::forward<CallbackT>(callback));
^
/Users/ahl/ros2_ws/install/rclcpp/include/rclcpp/node_impl.hpp:200:18: note: in instantiation of function template specialization 'rclcpp::create_service<std_srvs::srv::Trigger, std::__1::__bind<void (FirstNode::*)(std::__1::shared_ptr<rmw_request_id_t>, std::__1::shared_ptr<std_srvs::srv::Trigger_Request_<std::__1::allocator<void> > >, std::__1::shared_ptr<std_srvs::srv::Trigger_Response_<std::__1::allocator<void> > >), FirstNode *, const std::__1::placeholders::__ph<3> &, const std::__1::placeholders::__ph<2> &, const std::__1::placeholders::__ph<1> &> >' requested here
return rclcpp::create_service<ServiceT, CallbackT>(
^
/Users/ahl/happtec_ws/src/first_node_cpp/src/first_node.cpp:43:21: note: in instantiation of function template specialization 'rclcpp::Node::create_service<std_srvs::srv::Trigger, std::__1::__bind<void (FirstNode::*)(std::__1::shared_ptr<rmw_request_id_t>, std::__1::shared_ptr<std_srvs::srv::Trigger_Request_<std::__1::allocator<void> > >, std::__1::shared_ptr<std_srvs::srv::Trigger_Response_<std::__1::allocator<void> > >), FirstNode *, const std::__1::placeholders::__ph<3> &, const std::__1::placeholders::__ph<2> &, const std::__1::placeholders::__ph<1> &> >' requested here
server_ = this->create_service<std_srvs::srv::Trigger>("first_service", std::bind(&FirstNode::handle_service, this, _3, _2, _1));
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/functional:1645:15: note: candidate function not viable: no known conversion from 'std::__1::__bind<void (FirstNode::*)(std::__1::shared_ptr<rmw_request_id_t>, std ...
You might be interested in the discussion on this stack overflow post https://stackoverflow.com/questions/1... . Some people prefer lambda's instead of
std::bind
since C++14.