ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are two issues in your code :

  • The first one generates the following error : error: invalid use of non-static member function ‘bool test_services::TestServices::service_callback(const Request&, const Response&)’

It says that you tried to pass a non-static member function to advertiseService, which means you can't use it without an instaniated object of the class its contained in. If you look at the function prototype in the documentation :

ServiceServer ros::NodeHandle::advertiseService (   const std::string &     service,
bool(T::*)(MReq &, MRes &)  srv_func,
T *     obj 
)   
 \param service Service name to advertise on
 \param srv_func Member function pointer to call when a message has arrived
 \param obj Object to call srv_func on

So the second parameter must be a function pointer, and the third one must be an object of your class, like this :

  service_ = nodeHandle.advertiseService("service_name", &TestServices::service_callback, this);
  • The second issue is stated error : error: no declaration matches ‘bool test_services::TestServices::service_callback(std_srvs::SetBool::Request&, std_srvs::SetBool::Response&)’

In your hpp file you didn't declare the fonction correctly :

bool service_callback(const std_srvs::SetBool::Request &request,const std_srvs::SetBool::Response &response);

You have declared the parameters as const, you need to remove them.