ros2 add arguments to callback
Hi,
I am trying to add an argument to a ROS2 subscriber callback.
The main objective was to print the name of the topic from inside the callback, so I tried to follow this ROS1 example, but it's not working.
These are the relevant parts of my code
Creation of the subscriber
rclcpp::Subscription<std_msgs::msg::Header>::SharedPtr subscriber = this->create_subscription<std_msgs::msg::Header>(
topic_name,
std::bind(&MultiNode::topic_callback, this, std::placeholders::_1, topic_name));
Callback implementation
void MultiNode::topic_callback(const std_msgs::msg::Header::SharedPtr msg, std::string topic_name)
{
std::cout<< topic_name<<std::endl;
}
I get a very long sequence of errors which boils down to
error: no matching function for call to ‘rclcpp::AnySubscriptionCallback<std_msgs::msg::Header_<std::allocator<void> >, std::allocator<void> >::set(std::_Bind<std::_Mem_fn<void (MultiNode::*)(std::shared_ptr<std_msgs::msg::Header_<std::allocator<void> > >, std::__cxx11::basic_string<char>)>(MultiNode*, std::_Placeholder<1>, std::__cxx11::basic_string<char>)>)’
any_subscription_callback.set(std::forward<CallbackT>(callback));
See also #q289207