Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量值被循环中的最后一个值覆盖_C++_Loops_Pointers_Vector_Point Cloud Library - Fatal编程技术网

C++ 向量值被循环中的最后一个值覆盖

C++ 向量值被循环中的最后一个值覆盖,c++,loops,pointers,vector,point-cloud-library,C++,Loops,Pointers,Vector,Point Cloud Library,我有向量来存储点云的指针: std::vector<pcl::PointCloud<PointType>::Ptr> List; 在一个循环中,我试图将点云指针推回 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); while(condition) {... List.push_back(cloud); cloud-&g

我有向量来存储点云的指针:

std::vector<pcl::PointCloud<PointType>::Ptr> List;
在一个循环中,我试图将点云指针推回

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

while(condition)
{...    
    List.push_back(cloud);
cloud->clear();
}
它添加点云,但在每次迭代中,以前存储在向量中的所有值都将替换为新添加的值

假设我添加的最后一个点云的大小是400,我在迭代中添加了5个点云

循环之后,如果我检查存储的5个云的大小,结果将是 400 400 400 400 四百

有人知道如何修复它吗


感谢

看起来很像您只创建了一个点云,在循环的每次迭代中,您都会修改点云,将指向它的另一个指针添加到向量,然后重复

换句话说,所有向量条目都指向同一个云,因此每个修改当然都可以通过所有条目看到。

与其反复使用您的云变量(这是您的问题的根源),我将删除中间人

while(condition)
{   ...    
    List.emplace_back(new pcl::PointCloud<pcl::PointXYZ>);
}

…但在每次迭代中。。。我没有看到循环,那么我们如何识别问题呢?看起来您在反复使用同一个指针,并在循环的每次迭代中更改指向的对象。