ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Callback a template function

asked 2014-04-14 03:20:58 -0600

Maya gravatar image

updated 2016-10-24 09:03:29 -0600

ngrennan gravatar image


I have a template class Main<t>. I'm trying to use one of the function of this class as a callBack for a subscriber but I can't compile my code.

If I put a value in the template :

/home/ros/catkin_ws/src/Tobot/stalker/src/Stalker.cpp:27: undefined reference to `Main<pcl::PointXYZRGBA>::doWork(boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> const&)'

If I don't :

/home/ros/catkin_ws/src/Tobot/stalker/src/Stalker.cpp:27:86: erreur: ‘template<class T> class Main’ used without template parameters
make[2]: *** [Tobot/stalker/CMakeFiles/stalker_node.dir/src/Stalker.cpp.o] Erreur 1

The code for those errors would be respectively :

pointcloud_sub = my_node.subscribe<sensor_msgs::PointCloud2> ("cloud_filtered", 1, &Main<pcl::PointXYZRGBA>::doWork, &main);

pointcloud_sub = my_node.subscribe<sensor_msgs::PointCloud2> ("cloud_filtered", 1, &Main::doWork, &main);

Any help to get this working would be appreciated.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2014-04-16 23:02:00 -0600

Maya gravatar image

Silly me, I found the reason.

I put my template function in a cpp while they need to be inline in header file. That's it, that's all.

edit flag offensive delete link more

answered 2014-04-14 05:59:35 -0600

ahendrix gravatar image

The first call, with template parameters, is correct. The trouble is that the compiler isn't actually emitting the code for that callback function for some reason.

Probably the easiest (and hackiest) thing to do is to add a call to main.doWork() somewhere else in your program, just to convince the compiler to emit the code for it. Ideally, you could put this call on a code path that is unreachable, or call it with an empty argument so that it does nothing.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-04-14 03:20:58 -0600

Seen: 952 times

Last updated: Apr 16 '14