0
$\begingroup$

Rosanswers logo

Hi

I'm trying to make a subscription to a topic where the callback function takes 2 parameters, but I can't figure out how to write it. The function I want to call look like this:

void writeOut(const std_msgs::String::ConstPtr& msg, const sound_play::SoundClient& sound)
{
    // code here
}

The part where I try to subscribe to the topic looks like this:

int main(int argc, char **argv)
{
    ros::init(argc, argv, "name");
    ros::NodeHandle n;
    sound_play::SoundClient sound;

    ros::Subscriber sub = n.subscribe("topic-name",1000,writeOut);
    
    ros::spin();
    return 0;
}

I know that I can't just call the writeOut function like this. From searching around, I think I have to use something like boost::bind, but I can't figure out how to use it right.

The code is based on the tutorials, I'm just modifying it a bit.

Any help would be appreciated.

EDIT:

Using boost::bind as described in the answer narrowed the errors down, now I only get this error

'cref' was not declared in this scope

Do I need to use some external library?


Originally posted by Kinna on ROS Answers with karma: 35 on 2012-11-01

Post score: 3


Original comments

Comment by Lorenz on 2012-11-04:
cref is part of boost. I updated my answer.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Right, you need to use boost::bind. Try:

ros::Subscriber sub = n.subscribe("topic-name", 1000, 
    boost::bind(writeOut, _1, boost::cref(sound)));

Note that the call to cref is necessary because you want to pass a const reference instead of passing sound by value, i.e. copying it. If you want a normal reference, not a const reference, use ref.

Edit: I just updated my answer to explicitly qualify the namespace of cref (it should be in namespace boost). You need to include boost/ref.hpp.


Originally posted by Lorenz with karma: 22731 on 2012-11-01

This answer was ACCEPTED on the original site

Post score: 4

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.