Search code examples
c++shared-ptrpoint-cloud-library

Initialise PointCloudT::Ptr class member


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;

Solution

  • 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)