C++ 类成员未初始化

C++ 类成员未初始化,c++,initialization-list,C++,Initialization List,我有下面一段代码和简单的if语句:if(体素)where voxels\uuu0应该为NULL的地方失败了。守则: template<class T, typename REAL = float> class NDIMVoxelStructure { public: inline NDIMVoxelStructure (): voxels_(NULL){} inline virtual ~NDIMVoxelStructure (){ this->clear();

我有下面一段代码和简单的if语句:
if(体素)
where voxels\uuu0应该为NULL的地方失败了。守则:

template<class T, typename REAL = float>
class NDIMVoxelStructure
{
public:
    inline NDIMVoxelStructure (): voxels_(NULL){}
    inline virtual ~NDIMVoxelStructure (){ this->clear();}

    /////////////////ERROR occurs at if(voxels_) //////////////////
    inline void 
    clear (){if ( voxels_ ){delete[] voxels_; voxels_ = NULL;}} 


    inline void
    build (const std::vector<REAL> bounds, std::vector<int> num_of_voxels) {
        this->clear();
        // more code
    }

protected:
    T* voxels_;
};

Class ModelLibrary {

    ModelLibrary () {
        hash_table_.build (bounds_vector, num_of_cells_vector);
    }

    struct Quad{
            const ORROctree::Node::Data* first;
            const ORROctree::Node::Data* second;
            const float* f1;
            const float* f2;
    };

    typedef std::list<Quad > quad_list;
    // these two types hide base class types
    typedef std::map<const Model*, quad_list> HashTableCell; 
    typedef NDIMVoxelStructure<HashTableCell, float> HashTable;

protected:
    HashTable hash_table;
};

int main() {
    ModelLibrary library; 
}
模板
类NDIMVoxelStructure
{
公众:
内联ndimvoxelsstructure():体素{(NULL){}
内联虚拟~NDIMVoxelStructure(){this->clear();}
/////////////////if(体素)时出错//////////////////
内联空隙
清除(){if(体素){delete[]体素;体素=NULL;}
内联空隙
构建(const std::vector bounds,std::vector num_of_体素){
这个->清除();
//更多代码
}
受保护的:
T*体素;
};
类模型库{
模型库(){
哈希表构建(边界向量、单元数向量);
}
结构四元{
常量或八叉树::节点::数据*第一;
常量或八叉树::节点::数据*秒;
常量浮点*f1;
常量浮点*f2;
};
typedef std::list quad_list;
//这两种类型隐藏基类类型
typedef std::map HashTableCell;
typedef NDIMVoxelStructure哈希表;
受保护的:
哈希表;
};
int main(){
模型图书馆;
}

我在clear()方法中得到一个segfault。使用gdb,我得到了
体素的地址
设置为
0xa
,这很奇怪。我正在将其初始化为NULL,因此
if(体素)
应该只返回false。任何想法都会有帮助。考虑到这只是真实代码的一个“微型”版本,这让我发疯。是否您的实际代码正在调用
NDIMVoxelStructure
的复制构造函数(例如,通过返回
NDIMVoxelStructure
元素的函数),然后,
体素
未正确初始化

在前一种情况下,如果
voxels\uuuu
是一个指针,那么默认的复制构造函数最初会吞并复制空值,但可能会在幕后运行其他东西


我建议您也定义
NDIMVoxelStructure
的复制构造函数,并检查它是否被调用。

您正在尝试访问一个aleady deleted
NDIMVoxelStructure
对象,可能在一些您没有显示的代码中。请阅读以下内容:这个问题需要回答一个最简单可行的示例。您的示例编译并产生错误了吗?在方法
build()
中,您对
体素做了什么,它上面写着:
//更多代码
?@DarkFalcon我在clear方法中的“If”前面放了一个cout,然后打印出来,这样NDIMVoxelStructure就必须存在。