具有奇怪类型的成员函数导致回调函数不匹配

时间:2019-08-17 04:18:31

标签: c++ ros2

我有一个类,其中声明和实现位于.cpp文件中。 它具有一个名为imageCallback()的成员函数,该函数在构造函数中使用,如下所示

namespace viso2_ros {

class MonoOdometer : public rclcpp::Node, public OdometerBase
{
MonoOdometer(const std::string& transport, const rclcpp::NodeOptions& options)
  {
    camera_sub_ = image_transport::create_subscription(this, "image", &MonoOdometer::imageCallback, transport, 1);
  }

protected:
  void imageCallback(
      const sensor_msgs::msg::Image::ConstSharedPtr& image_msg) {
      // implementation of the image callback function
    }
}

} // end namespace

对我来说,这看起来不错,但是不起作用。

create_camera_subscription希望回调使用此签名:

const function<void (const shared_ptr<const sensor_msgs::msg::Image_<allocator<void> > > &)>

但它会获得此签名

void (viso2_ros::MonoOdometer::*)(const sensor_msgs::msg::Image::ConstSharedPtr &)

此签名是什么意思? namespace::classname::pointer?为什么不是功能?

(如果有兴趣,回调签名与他们在文档https://github.com/ros-perception/image_common/wiki/ROS2-Migration中使用的签名完全相同)

1 个答案:

答案 0 :(得分:2)

该签名意味着它是指向成员函数的指针,而不是指向函数的常规指针。为了调用<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" /> <div id="resources" class="offset"> <div class="fixed-background"> <div class="row dark text-center"> <div class="col-12"> <h3 class="heading">Sewa dari Tiga Kota Besar di Sumatera</h3> <div class="heading-underline"></div> </div> <div class="col-md-4"> <h3>Medan</h3> <div class="resource"> <img class="boxshadow" src="images/icon-medan.jpg"> </div> <a class="btn btn-secondary btn-sm" href="#" target="_blank">Pesan Disini</a> </div> <div class="col-md-4"> <h3>Palembang</h3> <div class="resource"> <img class="boxshadow" src="images/icon-palembang.jpg"> </div> <a class="btn btn-secondary btn-sm" href="#" target="_blank">Pesan Disini</a> </div> <div class="col-md-4"> <h3>Padang</h3> <div class="resource"> <img class="boxshadow" src="images/icon-padang.jpg"> </div> <a class="btn btn-secondary btn-sm" href="#" target="_blank">Pesan Disini</a> </div> <div class="col-lg-5 mx-auto"> <div class="resource"> <div class="heading-underline"></div> <!-- 16:9 aspect ratio, THIS IS THE SUITE ONE--> <div class="embed-responsive embed-responsive-16by9"> <!-- "..?playlist=(embedded-link)&loop=1" is for repeated the same videos that played --> <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/Y-1H27WxaJo?playlist=Y-1H27WxaJo&loop=1" allowfullscreen autoplay></iframe> </div> </div> </div> </div><!-- End Row Dark --> <div class="fixed-wrap"> <div class="fixed"><!-- class fixed is where we add the background image --> </div> </div> </div><!-- End Fixed Background --> </div>,它需要一个类MonoOdometer::imageCallback的实例进行调用,但是您的MonoOdometer只是希望它是一个独立的函数。两种解决方案是使create_camera_subscription成为imageCallback方法,或者传递函子(例如lambda),该函子将在其自己的调用运算符中调用static

静态:

imageCallback

Lambda:

static void imageCallback(...) {
  // ...
}