0

I'm trying to refactor the point cloud tutorial code into OO form.

Here's my class structure

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);
};

and usage

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

However I get the following runtime error

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

I believe my private class members are not initialised correctly but I am not sure how to fix this. I tried adding a constructor and initialising them there (this is my Java background coming into play) but that does not seem to be legal C++.

I had a look here and it says uninitialised references will not compile and objects are implicitly initialised. So I am a bit lost.

Can someone point me in the right direction?

Edit

I tried the constructor

PclRegister() {
    cloud_in = new PointCloudT;
}
error: no viable overloaded '=' cloud_in = new PointCloudT;
clicky
  • 865
  • 2
  • 14
  • 31
  • *PointCloudT::Ptr* is based on *boost::shared_ptr*. If you don't initialize them explicitly in the constructor of your class they are initialized to a nullptr by default. If you try to dereference a nullptr you will get the error you see, so make sure that you assign a valid pointer to your cloud_* variables before you use them. – the_summer Dec 25 '17 at 19:16
  • Thanks, but how do I initialise in a constructor? I've tried `cloud_* = new PointCloudT` and `cloud_*(new PointCloudT)` but both give compilation errors. – clicky Dec 25 '17 at 19:49

1 Answers1

1

You have to initialize the your shared_ptr properly. If you can do that in your constructor, do it like that:

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

If you want to initialize or update the pointers at a later stage you could probably use something like this:

cloud_in = PointCloudT::Ptr(new PointCloudT)
the_summer
  • 439
  • 3
  • 10