ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org

ROS2 Subscriber defined in Class (Win10, Visual Studio 14)

using std::placeholders::_1;

class MinimalSubscriber 
{
  public:
    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);
       }  
   }
   private:
       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<MinimalSubscriber> m_sub = std::make_shared<MinimalSubscriber>;
     rclcpp::shutdown();
     return 0;
}

Hi I tried to build this above example, but I got an error:

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!

I solved this problem.

Do you mind posting your solution here? This might be helpful for people stumbling upon the same error from search machines.

void topic_callback(const std_msgs::msg::String::SharedPtr& msg);

should be:

void topic_callback(const std_msgs::msg::String::SharedPtr msg)

Glad you found a solution to your problem.

In general we ask that you please ask questions on http://answers.ros.org following our support guidelines: http://wiki.ros.org/Support

ROS Discourse is for news and general interest discussions. ROS Answers provides a forum which can be filtered by tags to make sure the relevant people can find and/or answer the question, and not overload everyone with hundreds of posts.