0

我想在回调函数中使用成员类方法。下面的 C++ 代码是一个简单的订阅者,带有一个使用成员类方法的回调hello()

#include <functional>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using std::placeholders::_1;

class Subclass
{
private:
public:
    Subclass(/* args */);
    ~Subclass();
    std::string hello(std::string &s)
    {
        return "Hello " + s;
    }
};

class MinimalSubscriber : public rclcpp::Node
{
public:
    MinimalSubscriber()
        : Node("minimal_subscriber")
    {
        subscription_ = this->create_subscription<std_msgs::msg::String>(
            "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
    }

private:
    Subclass subclass_;
    void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
    {
        std::string s = subclass_.hello(msg->data);
        RCLCPP_INFO(this->get_logger(), "I heard: '%s'", s.c_str());
    }
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<MinimalSubscriber>());
    rclcpp::shutdown();
    return 0;
}

我构建了 C++ 代码并捕获了以下消息。我如何修复代码?使用 Ubuntu LTS 20.04 中的 ROS2 Foxy 构建。

/home/ubuntu/dev_ws/src/ros2_pigpio/src/subclass.cpp: In member function ‘void MinimalSubscriber::topic_callback(std_msgs::msg::String_<std::allocator<void> >::SharedPtr) const’:
/home/ubuntu/dev_ws/src/ros2_pigpio/src/subclass.cpp:35:50: error: passing ‘const Subclass’ as ‘this’ argument discards qualifiers [-fpermissive]
   35 |         std::string s = subclass_.hello(msg->data);
      |                                                  ^
/home/ubuntu/dev_ws/src/ros2_pigpio/src/subclass.cpp:15:17: note:   in call to std::string Subclass::hello(std::string&)’
   15 |     std::string hello(std::string &s)
      |                 ^~~~~
make[2]: *** [CMakeFiles/subclass.dir/build.make:63: CMakeFiles/subclass.dir/src/subclass.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/subclass.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

4

2 回答 2

0

首先,如果您将.msg->data作为字符串传递,请使用msg->data.c_str(). 因此,我相信您的std::string hello(std::string &s)方法在这种情况下必须是静态的,或者您必须使用构造函数创建子类的实例。

于 2020-10-30T09:04:26.890 回答
0

我可以建立以下一个。

#include <functional>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using std::placeholders::_1;

class Subclass
{
private:
public:
    Subclass(/* args */){};
    ~Subclass(){};
    std::string hello(std::string &s) const
    {
        return "Hello " + s;
    }
};

class MinimalSubscriber : public rclcpp::Node
{
public:
    MinimalSubscriber()
        : Node("minimal_subscriber")
    {
        subscription_ = this->create_subscription<std_msgs::msg::String>(
            "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
    }

private:
    Subclass subclass_;
    void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
    {
        std::string s = subclass_.hello(msg->data);
        RCLCPP_INFO(this->get_logger(), "I heard: '%s'", s.c_str());
    }
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<MinimalSubscriber>());
    rclcpp::shutdown();
    return 0;
}
于 2020-11-04T08:42:39.333 回答