I am using ros melodic on Ubuntu18.04 and C++14 and am trying to subscribe to a topic using a callback function in a Derived class. I know this is possible as a base class as seen in the 2.3.2 Class Methods section of the ros wiki, but I am trying to modify that for a derived class. I would like to be able to have multiple Scan_matching derived classes for different algorithms, and for main class to be able to select one. Here is what I have:
class Scan_Matching
{
public:
void get_transform(const sensor_msgs::PointCloud2ConstPtr& cloud_msg);
};
class ICP : public Scan_Matching
{
public:
void get_transform(const sensor_msgs::PointCloud2ConstPtr& cloud_msg);
};
int main(){
...
ScanMatching* scan_matching;
scan_matching = new ICP(...);
ros::Subscriber sub = nh.subscribe(topic, 100, &Scan_Matching::get_transform, scan_matching);
...
}
I am confident this is possible but I am not sure if I am implementing this correctly as I get a linking error from catkin:
[ 14%] Linking CXX executable /home/mike/documents/autonomous_sim/devel/lib/mike_av_stack/localization
CMakeFiles/mike_av_stack_node.dir/scripts/localization/localization.cpp.o: In function `main':
localization.cpp:(.text.startup+0x718): undefined reference to `Scan_Matching::get_transform(boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> const&)'
collect2: error: ld returned 1 exit status
mike_av_stack/CMakeFiles/mike_av_stack_node.dir/build.make:392: recipe for target '/home/mike/documents/autonomous_sim/devel/lib/mike_av_stack/localization' failed
make[2]: *** [/home/mike/documents/autonomous_sim/devel/lib/mike_av_stack/localization] Error 1
CMakeFiles/Makefile2:2493: recipe for target 'mike_av_stack/CMakeFiles/mike_av_stack_node.dir/all' failed
make[1]: *** [mike_av_stack/CMakeFiles/mike_av_stack_node.dir/all] Error 2
Makefile:145: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failed
To me this seems like the subscribe function is sending my get_transform function a parameter that is not the type
const sensor_msgs::PointCloud2ConstPtr& cloud_msg
(or in other words)
const boost::shared_ptr<sensor_msgs::PointCloud2 const>& cloud_msg
but instead the type
boost::shared_ptr<sensor_msgs::PointCloud2_std::allocator<void > const> const&
I tested this by trying the following code which compiles fine.
void callback(const boost::shared_ptr<sensor_msgs::PointCloud2 const>& cloud_msg){
}
int main(){
...
ros::Subscriber sub = nh.subscribe(topic, 100, callback);
...
}
I think it might be because of the derived class and I am calling the incorrect overloaded subscriber function. Can someone help me understand what I am doing wrong, and how to fix? Thank you