C++ How to set a callback to point to a non-static member function of an object instance?
[Ubuntu 13.10, Hydro Desktop-Full from source, C++]
I am having difficulty setting up callback functions for my ROS Topic subscribers, and would appreciate help.
Since all my Topics use the std_msgs::String
type, I've defined the class shown below with a generic callback function and an instance variable which identifies the specific Topic.
class myCallback {
const char * eventtype;
public:
myCallback(const char * eventType) : eventtype(eventType) {}
void generic_callback(const std_msgs::String::ConstPtr& msg) {
std::cerr << this->eventtype << " heard: " << msg->data.c_str() << std::endl;
}
};
For the ROS functionality in my project, I've created a separate class gwrbclass.cpp
& gwrbclass.h
, which include the myCallback
class. In the header file, I define myCallback *callbackList[NUM_EVENT_TYPES]
as a private variable. In the implementation, I instantiate the required myCallback objects for each entry in the callbackList and try to bind it to the node's subscriber object as shown below:
for (int j=0; j<NUM_EVENT_TYPES; j++) {
callbackList[j] = new myCallback(eventName);
subList[j] = (sNode[j])->subscribe(eventName, 1000, callbackList[j]->generic_callback); << ERROR OCCURRING HERE
}
I'm getting an error stating : reference to non-static member function must be called
, and as far as I can see, the method I'm calling is not static and therefore should work.
If I use subList[j] = (sNode[j])->subscribe(...., &callbackList[j]->generic_callback);
the error becomes : cannot create a non-constant pointer to member function
.
I'm confused as to why I'm getting this error (which I believe has something to do with std::bind, function typedef, wrapper functions, etc), and would appreciate some help as I can't figure out the syntax.