C++ 在列表中插入更改值c++;

C++ 在列表中插入更改值c++;,c++,list,push-back,C++,List,Push Back,我想创建多个“体素”对象并将它们放入列表中。但如果我这样做,体素存储的两个值就会改变。 以下是类定义: class Voxelization{ private: int sf_counter; bool subdiv; int seg_fac; int* labels; Pixel<int>* centers; int n_seeds; float* pixels; float* depth; int width; int height; int iterations; float

我想创建多个“体素”对象并将它们放入列表中。但如果我这样做,体素存储的两个值就会改变。 以下是类定义:

class Voxelization{

private:
int sf_counter;
bool subdiv;
int seg_fac;
int* labels;
Pixel<int>* centers;
int n_seeds;
float* pixels;
float* depth;
int width;
int height;
int iterations;
float r1;
float r2;
int env_size;
bool move_centers;
typename pcl::PointCloud<PointT>::ConstPtr& cloud;
typename NormalCloudT::ConstPtr normal_cloud;
}
我用这个代码插入它们:

  int seg_fac=100;
  int sf_counter=0;
  std::list<Voxelization> voxels
  for(sf_counter; sf_counter<seg_fac;sf_counter++){
      Voxelization voxel(input_cloud_ptr,seg_fac,sf_counter);
      voxels.push_back(voxel);
  }
int seg_fac=100;
int sf_计数器=0;
列出体素

对于(sf_counter;sf_counter,在摆弄了我的
构造函数
析构函数
操作符=
之后,我发现我只是忘了添加

int sf_counter;
bool subdiv;
int seg_fac;
到我的
复制构造函数
。 因此,工作的
复制构造函数
如下所示:

Voxelization::Voxelization(const Voxelization& voxel):sf_counter(voxel.sf_counter),subdiv(voxel.subdiv),seg_fac(voxel.seg_fac),labels(new int[voxel.width*voxel.height]), centers(voxel.centers),n_seeds(voxel.n_seeds),pixels(new float[voxel.width*voxel.height*3]),depth(new float[voxel.width*voxel.height]),width(voxel.width),height(voxel.height),iterations(voxel.iterations),r1(voxel.r1),r2(voxel.r2),env_size(voxel.env_size),move_centers(voxel.move_centers),cloud(voxel.cloud),normal_cloud(voxel.normal_cloud){
std::copy(voxel.labels, voxel.labels + voxel.width*voxel.height, labels);
std::copy(voxel.pixels, voxel.pixels + voxel.width*voxel.height*3, pixels);
std::copy(voxel.depth, voxel.depth + voxel.width*voxel.height, depth);
std::copy(voxel.centers, voxel.centers + voxel.width*voxel.height, centers);
}

你展示的构造函数不是很有趣-复制构造函数会很有趣。另外,类定义也会有帮助。我有根据的猜测:你的类有很多成员是指针,你在构造函数中分配,可能在析构函数中取消分配。你没有显式的复制构造函数,所以编译器生成了一个st复制指针。因此,您最终得到两个实例持有相同的指针,然后其中一个被销毁并释放该指针后面的内存,另一个则留下一个悬空的指针。另请参见:几乎正如我所想的:您毕竟有一个显式的复制构造函数,但它所做的只是复制指针,留下两个实例ng它们拥有相同的内存。Igor拥有它。当你插入列表时,你“复制构造”对象,然后删除原始对象。由于您的类有许多原始指针,它们将无法正确复制。确切地说,为什么它不复制两个特定项,我们无法在不查看它们的定义的情况下知道,但我们已经知道,大多数类在第一次复制时都会失败。如果您使用知道的类型,则不需要三个规则如何正确地复制它们自己。将原始指针更改为安全可复制的类型,无需编写复制构造函数、赋值运算符和析构函数。
Voxelization::Voxelization(const Voxelization& voxel):sf_counter(voxel.sf_counter),subdiv(voxel.subdiv),seg_fac(voxel.seg_fac),labels(new int[voxel.width*voxel.height]), centers(voxel.centers),n_seeds(voxel.n_seeds),pixels(new float[voxel.width*voxel.height*3]),depth(new float[voxel.width*voxel.height]),width(voxel.width),height(voxel.height),iterations(voxel.iterations),r1(voxel.r1),r2(voxel.r2),env_size(voxel.env_size),move_centers(voxel.move_centers),cloud(voxel.cloud),normal_cloud(voxel.normal_cloud){
std::copy(voxel.labels, voxel.labels + voxel.width*voxel.height, labels);
std::copy(voxel.pixels, voxel.pixels + voxel.width*voxel.height*3, pixels);
std::copy(voxel.depth, voxel.depth + voxel.width*voxel.height, depth);
std::copy(voxel.centers, voxel.centers + voxel.width*voxel.height, centers);
}