c++ 错误是boost shared_ptr和类继承

wfsdck30  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(139)

我正在遵循this tutorial并为我现有的ROS节点编写ROS nodelet Package 器类。但是当我运行test_nodelet.cpp时,每次尝试访问基类时,我都收到以下错误。我应该如何初始化TestNode?有人能给我指出正确的方向吗?
节点小程序:/usr/include/boost/智能指针/共享指针。hpp:734:类型名称增强::详细信息::sp成员访问::类型增强::共享指针::运算符-〉()常量[其中T = test::TestNode;类型名称增强::详细信息::sp成员访问::类型=测试::测试节点 *]:Assert“px!= 0”失败。
下面是我的头文件test.h:

#ifndef TEST_NODE_H_
#define TEST_NODE_H_

#include <test/TuningConfig.h>
#include <cv_bridge/cv_bridge.h>
#include <dynamic_reconfigure/server.h>
#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/RegionOfInterest.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/opencv.hpp>

namespace test {
class TestNode {
 public:
  TestNode(const std::string& cameraName);
  static TestNode& GetInstance(const std::string& cameraName = "");
  bool init();

 private:
  ros::NodeHandle nh;
  ros::Subscriber imageSub;
  ros::Publisher imagePub;
  void InputImageCallback(const sensor_msgs::Image::ConstPtr& msg);
  const std::string cameraName;
};
}  // namespace test

#endif  // TEST_NODE_H_

我的ROS节点看起来像下面的test_node.cpp:

#include <test/test_node.h>

namespace test {
TestNode& TestNode::GetInstance(const std::string& cameraName) {
  static TestNode instance(cameraName);

  return instance;
}

TestNode::TestNode(const std::string& cameraName)
    : cameraName(cameraName), nh(){};

bool TestNode::init() {
  imageSub = nh.subscribe(cameraName + "/image_raw", 10, &TestNode::InputImageCallback, this);
  imagePub = nh.advertise<sensor_msgs::Image>(cameraName + "/final_image", 100);
  return true;
}

void TestNode::InputImageCallback(const sensor_msgs::Image::ConstPtr& msg) {
  cv_bridge::CvImageConstPtr cvPtr = cv_bridge::toCvShare(msg, sensor_msgs::image_encodings::BGR8);
  TestNode::GetInstance().SetCurrentImage(cvPtr->image);
  }
 }

现在我尝试用下面的方式将测试类 Package 在nodelet类中(test_nodelet. cpp)-

#include "test/test_node.h"
#include "nodelet/nodelet.h"
#include "pluginlib/class_list_macros.h"

namespace test {

class TestNodelet : public nodelet::Nodelet {
 public:
  TestNodelet(){};
  ~TestNodelet(){}

  virtual void onInit() {
    ros::NodeHandle& nh = this->getPrivateNodeHandle();

    std::string name = nh.getUnresolvedNamespace();
    int pos = name.find_last_of('/');
    name = name.substr(pos + 1);

    if (node_->init()) {
      NODELET_INFO_STREAM("Initialised. [" << name << "]");
    } else {
      NODELET_ERROR_STREAM("Couldn't initialise. ["<< name << "]");
    }
  }

 private:
  boost::shared_ptr<test::TestNode> node_;
};
}  // namespace test

PLUGINLIB_EXPORT_CLASS(test::TestNodelet, nodelet::Nodelet);
rqmkfv5c

rqmkfv5c1#

我认为代码的问题似乎是TestNodelet类中的node_ member变量在onInit()函数中访问之前没有初始化。您需要示例化TestNode的一个示例并将其赋给node_。

相关问题