2016-07-20 15 views
-1

複数の「ボクセル」オブジェクトを作成してリストに入れたい。しかし、もし私がそうするならば、ボクセルによって記憶された値のうちの2つが変化する。 ここでは、クラスの定義である:リストを挿入すると値が変わるC++

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; 
} 

そして、これはコピーコンストラクタです:

Voxelization::Voxelization(const Voxelization& voxel):labels(voxel.labels), centers(voxel.centers),n_seeds(voxel.n_seeds),pixels(voxel.pixels),depth(voxel.depth),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){} 

そして、このコードで、私はそれらを挿入しています:

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); 
    } 

私が見れば単一のボクセルオブジェクトを作成した後は、seg_facとsf_counterの値が正しくなります。しかし、私はそれらを挿入し、リストでそれらを検索する場合は、8744512または-236461096のようなものに変更されます。 私の質問は、どこから来たのですか?

ご協力いただきありがとうございます。

+3

あなたが表示され、コンストラクタは非常に興味深いものではありません - コピーコンストラクタは次のようになります。また、クラス定義が役に立ちます。私の推測した推測:あなたのクラスには、ポインタであるメンバーがたくさんあります。あなたはコンストラクターで割り振り、デストラクターでは割り当てを解除する可能性があります。あなたは明示的なコピーコンストラクタを持っていないので、コンパイラが生成したコンストラクタは単にポインタをコピーします。つまり、同じポインタを保持する2つのインスタンスで終わると、そのうちの1つが破棄され、そのポインタの後ろにあるメモリの割り当てが解除され、もう1つにダングリングポインタが残されます。 –

+0

'seg_fac'はあなたのクラスでどのように宣言されていますか? – CiaPan

+0

'int * empty_labels = new int [width * height];' - なぜstd :: vector empty_labels(width * height、0) 'を使っていないのですか? 'new []'を行うコードの残りの部分と同じことです。あなたは 'std :: vector'を使うことができ、これを行う必要はありません。これは重要ではないかもしれませんが、 'voxels.push_back(voxel) 'に問題がある理由の一部となるかもしれません。 – PaulMcKenzie

答えて

0

constructersdestructorsoperator=と周りいじる後、私は、私は単に私のcopy-constructor

int sf_counter; 
bool subdiv; 
int seg_fac; 

を追加するのを忘れていることが分かりました。 ので、作業copy-constructorは、次のようになります。

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); 
} 
関連する問題