我正在尝试重构 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/