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

ROS2 Node Definition in Class

class Listener
{
public:
Listener()
{
m_rosNode = rclcpp::Node::make_shared(“Listener”);
sub_ = m_rosNode->create_subscription<std_msgs::msg::String>(“chatter”, &Listener::callback);
rclcpp::spin(m_rosNode);
}
void callback(const std_msgs::msg::String::SharedPtr msg)
{
RCLCPP_INFO(m_rosNode->get_logger(), “I heard: [%s]”, msg->data.c_str());
}

private:
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_;
std::shared_ptr<rclcpp::Node> m_rosNode;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
Listener lister;
rclcpp::shutdown();
return 0;
}

Hi I am a beginner, I wrote some codes described above. However, after ament build, it comes out an error:
5>C:\dev\ros2_2018_03_07_fastrtps\ros2-windows\include\rclcpp/function_traits.hpp(52): error C2825: ‘FunctionT’: must be a class or namespace when followed by ‘::’ [D:\sr
c\ros2_pub_sub_test\build\ros2_pub_sub_test\listener_1.vcxproj]

can anyone help me! Thanks in advance.

Please ask on answers.ros.org with the appropriate tags like ros2, and then include more details, like the OS you’re using, the compiler version you’re using, which version of ROS 2 you’re using, and how you installed ROS 2.