Using Ptr in a class and a vector

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Ptr in a class and a vector

Li Yuhang
Hi,
I have a problem.
I cannot correctly push back to a vector a class which has a Ptr point cloud as member variable. More specifically, I have a plane class:
class PlanePrimitives
{
 private:
  PointCloudType::Ptr cloud;
...
 public:
        void setCloud(PointCloudType::Ptr cld);
...
}

void PlanePrimitives::setCloud(PointCloudType::Ptr cld)
{
// Here I have tried many other method to set cloud, such as
// cloud = cld;
// cloud = PointCloudType::Ptr(new PointCloudType(*cld));
// and so on.
// They all works in copy cld to cloud.
        cloud.reset(new PointCloudType);
        for (size_t i = 0; i < cld->size();i++)
        {
                cloud->push_back(cld->points[i]);
        }
}

It does not work in blow code somewhere:
{
    PointCloudType::Ptr cld;
    ...    // Fill cld with some points here.
    std::vector<PlanePrimitives, Eigen::aligned_allocator<PlanePrimitives>> vecPlanesPrimitives;
    PlanePrimitives plane;  // Initialize its other member variables.
    plane.setCloud(cld); // When debugging, here points of plane.cloud is the same as cld's.
    vecPlanesPrimitives.push_back(plane);
    // Here vecPlanesPrimitives[0].cloud is empty!!!! So are its other member variables.
}

Would you point me out what I have missed. Should I do something special in this circumstance?
Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Using Ptr in a class and a vector

VictorLamoine
Administrator
Hello,

You don't want to allocate memory when using setCloud. All you want to do is copy the cld point cloud address into your private pointer cloud.

It should look like:

void PlanePrimitives::setCloud(PointCloudType::Ptr cld)
{
  cloud = cld;
}
Bye