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

Service Advertice and Class Usage

asked 2012-09-05 23:44:40 -0500

cheour gravatar image


When we try to advertise a service we normally use as shown in ros tutorials:

 ros::ServiceServer service = n.advertiseService("add_two_ints", add);

This is with normal function way.

And if we want to use with class, documentation says use like this:

AddTwo a;
 ros::ServiceServer ss = n.advertiseService("add_two_ints", &AddTwo::add, &a);

Which means we need to initialize an object first.

But suppose I want to use everything related with ros in class and suppose my code is like this:

  ros::ServiceServer ss = n.advertiseService("add_two_ints", &write);
  // write() is member of Comm class.

In my mind, I should have been use like this, but of course this kind of usage gave me error. Is there any possibility to use service advertise in class as describe. Do I need to initialize the class always?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-09-06 00:07:19 -0500

dornhege gravatar image

Use it the same way as above with: &Comm::write, this.

Also you might want to store the ServiceServer as a member variable as otherwise it will go out of scope.

edit flag offensive delete link more

Question Tools


Asked: 2012-09-05 23:44:40 -0500

Seen: 106 times

Last updated: Sep 06 '12