Subscribers quiz on ROS Basics c++: bind statement not working

Hello! I am experiencing difficulties with the first quiz in the ROS Basic in 5 days (C++) course.

The program is expected to pass the publisher to the subscriber callback function, so that the callback can control the robot. Here’s the code:

#include <geometry_msgs/Twist.h>

#include <ros/ros.h>

#include <sensor_msgs/LaserScan.h>

#include <std_msgs/Int32.h>

void laserScanCallback(const sensor_msgs::LaserScan::ConstPtr &msg, int a) {

  ROS_INFO("Received smth %n", a);


int main(int argc, char **argv) {

  ros::init(argc, argv, "topics_quiz_node");

  ros::NodeHandle nh;

  //   ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);

  ros::Publisher pub0 = nh.advertise<std_msgs::Int32>("testing", 1000);

  int a = 4;

  ros::Subscriber sub = nh.subscribe("/kobuki/laser/scan", 1000, boost::bind(&laserScanCallback, -1, a));


  return 0;


However if I use the bind function I receive an error:

/home/user/catkin_ws/src/topics_quiz/src/topics_quiz_node.cpp: In function 'int main(int, char**)':
/home/user/catkin_ws/src/topics_quiz/src/topics_quiz_node.cpp:26:104: error: no matching function for call to 'ros::NodeHandle::subscribe(const char [19], int, boost::_bi::bind_t<void, void (*)(const boost::shared_ptr<const sensor_msgs::LaserScan_<std::allocator<void> > >&, int), boost::_bi::list2<boost::_bi::value<int>, boost::_bi::value<int> > >)'
   26 |   ros::Subscriber sub = nh.subscribe("/kobuki/laser/scan", 1000, boost::bind(&laserScanCallback, -1, a));

Is there maybe another way to perform this? OR maybe I’m missing some important principles of C++ programming? I’m new to C++, so sorry if that’s an obvious mistake.

Hi, welcome to the community!

I recommend getting rid of the boost::bind in the definition of your subscriber. You can try replacing it with

  ros::Subscriber sub = nh.subscribe("/kobuki/laser/scan", 1000, laserScanCallback);

Correct me if im wrong but I think the placeholder should have been _1 and not -1 since it will take the first parameter of the void laserScanCallback