Subscribing to templated message type
I have a templated class that accepts a message type as a parameter and a topic as an argument. I define the subscriber in the constructor:
subscriber = n.subscribe(sub_topic, 1000, &TemplatedClass::callbackFunction, this);
and the callback function is defined as:
template<class T>
void TemplatedClass<T>::callbackFunction(const typename T::ConstPtr& incoming_msg)
{
...
}
But the callback function is never called. When I subscribe to the topic from main
using a subscriber that isn't from a templated class, the callback function is called, so the topic is being published to. I considered that it could be that the signature const typename ...
might be causing issues, but of course the program won't compile without typename
. Any ideas?
Turns out the issue was that I'd instantiated two subscribers in my constructor, but forgot to adjust the name of the second one. I had:
subscriber1 = n.subscribe(sub_topic1, 1000, &TemplatedClass::callbackFunction1, this);
subscriber1 = n.subscribe(sub_topic2, 1000, &TemplatedClass::callbackFunction2, this);
so subscriber1
was being overwritten and subscriber2
was never defined. My code was otherwise right, so I'm leaving it here in case anybody else needs the syntax for subscribers in templated classes.