我有一个模板化的迭代器类:
template <class number_type, class label_type>
class DepthIterator {
public:
// CONSTRUCTOR / COPY / ASSIGN / DECONSTRUCTOR
DepthIterator() : ptr_(NULL) {}
DepthIterator(Node<number_type, label_type>* n) : ptr_(n) {}
DepthIterator(const DepthIterator& old) : ptr_(old.ptr_), visited(old.visited) {}
~DepthIterator() {}
// OPERATORS
DepthIterator& operator=(const DepthIterator& old) { ptr_ = old.ptr_; return *this; }
const Point<number_type>& operator*() const { return ptr_->pt; }
bool operator== (const DepthIterator& rgt) { return ptr_ == rgt.ptr_; }
bool operator!= (const DepthIterator& rgt) { return ptr_ != rgt.ptr_; }
// ++ DEPTH FIRST ITERATOR
DepthIterator& operator++() {
if (ptr_->children[0] == NULL && ptr_->children[1] == NULL) {
// we're at a leaf, so go up one and back down and add to visited list
visited.push_back(ptr_);
ptr_ = ptr_->parent;
return *this;
}
else {
// do stuff
}
private:
Node<number_type, label_type>* ptr_;
std::vector<Node<number_type,label_type>* > visited; // causes problem
};
当第二行到最后一行 std::vector<Node<number_type, label_type,>* > visited;
被删除时,我的程序编译并运行正常(假设运算符被注释掉) . 但是,在此类中将此行添加到private会导致我的程序编译但在运行中途崩溃 . 中止(核心倾倒) .
我做了一些测试,我添加到这个类的任何std :: vector变量似乎都会导致它崩溃而不管它是什么 .
为什么变量导致这个?我该如何修复它?