Ask Your Question

Problem Using Class function as callback

asked 2013-06-06 06:01:13 -0500

kaoao gravatar image

updated 2014-01-28 17:16:47 -0500

ngrennan gravatar image

I'm trying to encapsulate nodes to support dynamic node creation. When I try to register a callback from inside a class using one of that classes functions I get a compile error. The following code works correctly if I do static callback().

class node {
        ros::NodeHandle *n;
        node(int argc, char **argv, std::string s) {
            n = NULL;
            ros::init(argc, argv, s);

        bool node::create() {
        if (n == NULL) {
        n = new ros::NodeHandle();
        return true;

        return false;

    ros::NodeHandle getHandle() {return *n;}

class coordinator
        node myNode;
        ros::Subscriber sub;

        void setup() {
            //assume myNode() has been called correctly
            //this does not compile
            sub = myNode.getHandle().subscribe("telemetry", 1000, &coordinator::callback);
        void callback(const TelemetryUpdate::ConstPtr& msg) {;/** stuff */}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-06-06 06:36:11 -0500

mjcarroll gravatar image

If you review the ROS Cpp Publisher and Subscriber Overview, you will find that you need to also pass a reference to the instance of the object.

In this case, you would probably want to do:

sub = myNode.getHandle().subscribe("telemetry", 1000, &coordinator::callback, this);
edit flag offensive delete link more


Thanks, I forgot about the this keyword.

kaoao gravatar image kaoao  ( 2013-06-06 07:56:01 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-06-06 06:01:13 -0500

Seen: 1,379 times

Last updated: Jun 06 '13