Register protected callback in base class

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

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{
Base () {}
void callback(const CameraInfoConstPtr & msg) { ... }
ros::Subscriber sub;

class Derived : public Base{
Derived() {
NodeHandle nh;
sub = nh.subscribe("topic", 1,

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


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:

jarvisschultz gravatar image jarvisschultz  ( 2015-01-28 13:30:20 -0500 )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 image jarvisschultz  ( 2015-01-28 13:36:34 -0500 )edit