Register protected callback in base class

asked 2015-01-28 05:01:19 -0600

daniel_maier gravatar image

Hi! The compiler complains about me trying to subscribe to messages and register a parent's class protected callback method. Here is the (simplified) code:

class Base{
public:
Base () {}
protected:
void callback(const CameraInfoConstPtr & msg) { ... }
ros::Subscriber sub;
}

class Derived : public Base{
public:
Derived() {
NodeHandle nh;
sub = nh.subscribe("topic", 1,
        &Base::callback,
dynamic_cast<Base*>(this));
}
}

And this is the compiler (g++) error:

error: 'void Base::callback(const CameraInfoConstPtr&)' is protected

The problem dissolves if I make the callback public, but I do not want to do that. Can somebody explain why it does not work and show a workaround? Thank you!

edit retag flag offensive close merge delete

Comments

I believe this is more of a C++ problem and less of a ROS problem. It seems to me that changing &Base::callback to &Derived::callback should fix the issue: http://stackoverflow.com/questions/14...

jarvisschultz gravatar imagejarvisschultz ( 2015-01-28 13:30:20 -0600 )edit

This may not be exactly what you want... I'm not sure what you are trying to accomplish, but I do believe the error is related to the context inside of Derived

jarvisschultz gravatar imagejarvisschultz ( 2015-01-28 13:36:34 -0600 )edit