Ask Your Question
3

Multiple parameters in subscribe method's callback function [closed]

asked 2012-11-01 23:23:30 -0500

Kinna gravatar image

updated 2012-11-02 01:08:31 -0500

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?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by demmeln
close date 2014-03-29 11:06:45

Comments

cref is part of boost. I updated my answer.

Lorenz gravatar imageLorenz ( 2012-11-04 03:46:25 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2012-11-01 23:54:55 -0500

Lorenz gravatar image

updated 2012-11-04 03:46:02 -0500

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.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-01 23:23:30 -0500

Seen: 2,459 times

Last updated: Mar 28 '14