How to use callback in a class containing ROS2 Node (Win10 X64, Visual Studio 14)

asked 2018-05-23 10:04:52 -0600

ycs134470 gravatar image

using std::placeholders::_1;

class MinimalSubscriber {


MinimalSubscriber() {}

init(int m_agrc, char** m_argv)


rclcpp::init(m_argc, m_argv);

m_rosnode = std::make_shared<rclcpp::node>(“node_sub”);

sub = m_rosnode->create_subscription<std_msgs::msg::string>( “topic”,

std::bind(&MinimalSubscriber::topic_callback, this, _1));

while(rclcpp::ok()) { rclp::spin(m_rosnode); }



void topic_callback(const std_msgs::msg::String::SharedPtr msg) { RCLCPP_INFO(m_rosnode->get_logger(), “I heard: ‘%s’”, msg->data.c_str()) }

rclcpp::Publisher<std_msgs::msg::string>::SharedPtr m_rosnode;

rclcpp::Subscription<std_msgs::msg::string>::SharedPtr sub;


int main(int argc, char * argv[])


rclcpp::init(argc, argv);

std::shared_ptr m_sub = std::make_shared;


return 0;


Hi I tried to build this above example, but I got an error: C:\dev\ros2_fastrtps\ros2-windows\include\rclcpp\subscription_factory: error C2672: ‘rclcpp::AnySubscriptionCallback<messaget,alloc>::set’: no matching overloaded function found.

this error was caused by this line : subscription = m_rosnode->create_subscription<std_msgs::msg::string>( “topic”, std::bind(&MinimalSubscriber::topic_callback, this, _1));

Can anyone help me? Thanks in advance!

edit retag flag offensive close merge delete


This was cross-posted with a solution:

William gravatar image William  ( 2018-05-23 18:31:45 -0600 )edit