0
$\begingroup$

Rosanswers logo

I have a subscriber that is currently set up with a class method that is called when a message is received from a topic. The code is pretty standard:

ros::Subscriber StageOdo_sub = n.subscribe<nav_msgs::Odometry>("robot_0/odom",1000, &MyClass::StageOdom_callback,this);

I need to shift the callback function to a superclass / base class. However, this generates errors and notes that say that template argument substitution failed. How can I ensure that the superclass function is called?


Originally posted by falseazure on ROS Answers with karma: 65 on 2014-08-21

Post score: 3

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

I assume this is e. g. of type MyDerivedClass* where MyDerivedClass inherits from MyClass and the method StageOdom_callback of class MyClass is protected or public (not private!).

If so have you tried:

ros::Subscriber StageOdo_sub = n.subscribe("robot_0/odom",1000, &MyClass::StageOdom_callback, dynamic_cast<MyClass*>( this ) );

If your compiler does not provide dynamic_cast you could also use reinterpret_cast<MyClass*>. However, dynamic_cast is safer as it checks at runtime that the class of the object pointed to by this really inherits from MyClass...

Edit:

I just noticed: you do not need to explizitly select the template type (<nav_msgs::Odometry>) like you do with advertise. The Template type is implizitly selected from the callback argument type...


Originally posted by Wolf with karma: 7555 on 2014-08-22

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by falseazure on 2014-08-23:
Great answer, thank you! I appreciate the heads up about selecting the template type, too.

Comment by wdudzik on 2019-12-05:
Hello! I realize this is a pretty old topic but this was incredibly helpful to me when trying to create a timer using a parent callback. I was curious why you have to cast this. Can anyone shed some light on that?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.