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

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 {
    private:
        ros::NodeHandle *n;
    public:
        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
{
    private:
        node myNode;
        ros::Subscriber sub;

    public:
        void setup() {
            //assume myNode() has been called correctly
            myNode.create();
            //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
1

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

Comments

Thanks, I forgot about the this keyword.

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

Question Tools

Stats

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

Seen: 1,708 times

Last updated: Jun 06 '13