我有以下代码和简单的 if 语句:if (voxels_)
where voxels_ should be 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;
}
我在 clear() 方法中遇到了段错误。使用 gdb 我得到的地址voxels_
设置为0xa
很奇怪。我将它初始化为NULL,所以if (voxels_)
应该简单地返回false。任何想法都会有所帮助。这真让我抓狂