我正在尝试将point cloud tutorial code重构为OO表单。
这是我的班级结构
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.
我相信我的私人班级成员没有正确初始化,但我不知道如何解决这个问题。我尝试添加一个构造函数并在那里初始化它们(这是我的Java背景发挥作用)但这似乎不是合法的C ++。
我看了一下here,它说未初始化的引用不会编译,对象也会被隐式初始化。所以我有点失落。
有人能指出我正确的方向吗?
修改
我尝试了构造函数
PclRegister() {
cloud_in = new PointCloudT;
}
error: no viable overloaded '=' cloud_in = new PointCloudT;
答案 0 :(得分:1)
您必须正确初始化您的shared_ptr。如果您可以在构造函数中执行此操作,请执行以下操作:
PclRegister()
: cloud_in(new PointCloudT),
cloud_tr(new PointCloudT),
cloud_icp(new PointCloudT)
{}
如果你想在稍后阶段初始化或更新指针,你可能会使用这样的东西:
cloud_in = PointCloudT::Ptr(new PointCloudT)