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

标签 c++ boost shared-ptr ros

我正在关注this tutorial并为我现有的 ROS 节点编写 ROS Nodelet 包装器类。但是,当我每次尝试访问基类时运行 test_nodelet.cpp 时,都会出现以下错误。我应该如何初始化TestNode?有人可以指出我正确的方向吗 -

nodelet: /usr/include/boost/smart_ptr/shared_ptr.hpp:734: typename boost::detail::sp_member_access::type boost::shared_ptr::operator->() const [with T = test::TestNode; typename boost::detail::sp_member_access::type = test::TestNode*]: Assertion `px != 0' failed.

以下是我的头文件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);
  }
 }

现在我尝试通过以下方式将测试类包装在 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);

最佳答案

我认为代码的问题似乎是TestNodelet类中的node_成员变量在onInit()函数中访问之前没有被初始化。您需要实例化一个 TestNode 实例并将其分配给 node_。 添加node_ = boost::make_shared<test::TestNode>(name);之后 name = name.substr(pos + 1);

关于c++ - 错误是boost shared_ptr和类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75749947/

相关文章:

c++ - 你什么时候会使用 std::auto_ptr 而不是 boost::shared_ptr?

c++ - gcc 6.1.0 段错误 - gcc 错误?

c++ - 数组指针的影响

C++ int 与 long

c++ - 抽象线程相关的 STL 和 Boost 类型和方法

c++ - boost shared_ptr<XX> 是线程安全的吗?

c++ - std::optional 和 boost::optional 是否遵守托管对象的对齐限制?

c++ - 在 std::shared_ptr 上使用 .reset() 是否会删除所有实例

c++ - shared_ptr : "is not a type" error

c# - x86 和 x64 之间的项目组织