Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Alternatively, use a lambda, since lambdas can access variables of the same scope by reference / value

ros::Publisher pub = ...;
ros::Subscriber sub = nh.subscribe<my_package::my_message>("/my_topic", 1,
        [&](const my_package::my_message::ConstPtr& msg)
        {
            pub.publish(...);
        });

Alternatively, use a lambda, since lambdas can access variables of the same scope by reference / value

ros::Publisher pub = ...;
ros::Subscriber sub = nh.subscribe<my_package::my_message>("/my_topic", 1,
        [&](const my_package::my_message::ConstPtr& msg)
msg) //replace & with = to access by value
        {
            pub.publish(...);
        });

Alternatively, use a lambda, since lambdas can access variables of the same scope by reference / value

ros::Publisher pub = ...;
ros::Subscriber sub = nh.subscribe<my_package::my_message>("/my_topic", 1,
        [&](const my_package::my_message::ConstPtr& msg) //replace & with = to access by value
        {
            pub.publish(...);
        });
ros::spin()

Alternatively, use a lambda, since lambdas can access variables of the same scope by reference / value

ros::Publisher pub = ...;
ros::Subscriber sub = nh.subscribe<my_package::my_message>("/my_topic", 1,
        [&](const my_package::my_message::ConstPtr& msg) //replace & with = to access by value
        {
            pub.publish(...);
        });
ros::spin()