c++ - 初始化 PointCloudT::Ptr 类成员

标签 c++ shared-ptr point-cloud-library

我正在尝试重构 point cloud tutorial code变成面向对象的形式。

这是我的类结构

class PclRegister {
private:
    // The point clouds we will be using
    PointCloudT::Ptr cloud_in;  // Original point cloud
    PointCloudT::Ptr cloud_tr;  // Transformed point cloud
    PointCloudT::Ptr cloud_icp; // ICP output point cloud
    pcl::console::TicToc time;
public:
    void registerFixedSurface(std::string path);
    Eigen::Matrix4d applyTransformation();
    void performIcp(Eigen::Matrix4d transform, int iterations);
    void print4x4Matrix(const Eigen::Matrix4d & matrix);
};

和用法

goicpz::PclRegister pclRegister;
pclRegister.registerFixedSurface(argv[1]);
Eigen::Matrix4d transform = pclRegister.applyTransformation();
pclRegister.performIcp(transform, iterations);

但是我得到以下运行时错误

Assertion failed: (px != 0), function operator*, file /project/build/Boost/install/include/boost/smart_ptr/shared_ptr.hpp, line 704.

我相信我的私有(private)类成员没有正确初始化,但我不确定如何解决这个问题。我尝试添加一个构造函数并在那里初始化它们(这是我的 Java 背景发挥作用)但这似乎不是合法的 C++。

我看了here它说未初始化的引用不会编译并且对象被隐式初始化。所以我有点迷路。

有人能指出我正确的方向吗?

编辑

我试过构造函数

PclRegister() {
    cloud_in = new PointCloudT;
}
error: no viable overloaded '=' cloud_in = new PointCloudT;

最佳答案

您必须正确初始化您的 shared_ptr。如果你可以在你的构造函数中做到这一点,那么就这样做:

PclRegister()
  : cloud_in(new PointCloudT),
    cloud_tr(new PointCloudT),
    cloud_icp(new PointCloudT)
{}

如果你想在稍后阶段初始化或更新指针,你可能会使用这样的东西:

cloud_in = PointCloudT::Ptr(new PointCloudT)

关于c++ - 初始化 PointCloudT::Ptr 类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971220/

相关文章:

c++ - 如何清除输入行,而不仅仅是单个字符

c# - 如何为 C# 项目生成 .tlh 文件

c++ - 具有动态列数的 QML TableView

c++ - 如何通过 unsigned long 将 std::shared_ptr 传递给回调?

c++ - 如何使用qmake将pcl库成功添加到qt项目中

c++ - 来自 Objective-C 的 pcl::io::loadPCDFile 以 EXC_BAD_ACCESS 结尾

c++ - Qt Creator 在 linux mint 64 位中找不到 CMAKE_CXX_COMPILER 编译器

c++ - std::shared_ptr 在空指针上调用非默认删除器

C++ shared_ptr<Base> 指针访问冲突

c++ - 在 PCL 中从无组织的点云生成图像