-4

I have the vector to store pointers of point clouds:

std::vector<pcl::PointCloud<PointType>::Ptr> List;

In a loop I am trying to push_back point cloud pointers in it.

pcl::PointCloud<pcl::PointXYZ>::Ptr  cloud(new pcl::PointCloud<pcl::PointXYZ>);

while(condition)
{...    
    List.push_back(cloud);
cloud->clear();
}

It adds point clouds, but at each iteration all values stored in the vector previously are replaced by the newly added value.

So lets say the size of the last point cloud I added is 400 and I added 5 point clouds in the iteration.

After the loop, if I check the sizes of stored 5 clouds, the results will be 400 400 400 400 400

Does anyone have an idea how to fix it?

Thanks

2 Answers2

4

It looks very much like you only create one point cloud, and in every iteration of your loop, you modify the point cloud, add a(nother) pointer to it to the vector, and repeat.

In other words, all your vector entries point to the same cloud, so every modification is of course visible through all entries.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

Rather than re-using your cloud variable over and over, which is the source of your issue, I would cut out the middle man

while(condition)
{   ...    
    List.emplace_back(new pcl::PointCloud<pcl::PointXYZ>);
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218