How to return values from callback function

asked 2017-12-25 09:06:36 -0500

can-43811 gravatar image

updated 2017-12-25 20:46:12 -0500

Hi All,

I am using below function in future to know the asynchronus result of the thread but the thing is this callback will have no return type as ros::spin needs to be installed here. I would like to know how to design this function so that return type can be given to below function including ros::spin.

void subscriber :: handlecallback( )
ros::Subscriber sub = n.subscribe("chatter", 1000, &Listener::callback, &listener);

ros::Subscriber sub1 = n.subscribe("ABC", 1000, &Listener::callback, &listener);

ros::Subscriber sub1 = n.subscribe("CBA", 1000, &Listener::callback, &listener);


std::future<void> result = std::async(&subscriber::handlecallback, &abc, rand()%20);

        try {
            std::cout << "Task returned " << result.get() << "\n";// result.get can't have void here.
        } catch (const std::exception &ex) {
            std::cout << "Task exited with exception: " << ex.what() << "\n";
            std::cerr << "Thread exited with exception: " << ex.what() << "\n";
edit retag flag offensive close merge delete



Can you explain a little more what you're trying to achieve here. Topic callback functions cannot return a value, they are defined as void by ROS. I also don't understand why you are creating more subscribers in a callback function they will be destroyed as soon as the function returns.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2017-12-29 11:01:42 -0500 )edit