class foo{
vector<foo*>* queue;
vector<int> pos;
foo(vector<foo*>* queue, vector<int> pos){
this->queue=queue;
this->pos=pos;
}
public:
foo(vector<foo*>* queue){
this->queue=queue;
}
void init(){
vector<int> posNew = pos;
//Create Binary Tree Children of the state FOO
posNew.push_back(/* An Additional Value*/)
foo newFoo(queue, posNew);
queue->push_back(&newFoo);
}//Here the variables newFoo and posNew are out of scope so they are deleted even from the queue
}
class bar{
vector<foo*> queue; //Assume that queue has a root node added to it.
bar(){
for(unsigned int i=0; i<queue.size();i++){
queue[i]->init();// Somewhere along when the third element is calculated the value overflows since I assume the object are deleted
}
}
}
我正在尝试使用带队列的BFS搜索来解决问题。但是我无法让队列工作,因为我创建的对象子对象超出了范围。任何帮助,将不胜感激。如何访问超出范围的变量?
编辑: 在我的实际代码中,我有麻烦,因为当对象超出范围它显示我这些内存分配。
这个绿色部分是根节点的位置,红色部分是子节点的期望数据应该是的位置,但现在它被删除。
为什么你需要'init()'?把东西移动到ctor('foo(vector *)')。 –
GingerPlusPlus
在'bar'的构造函数中,'queue'将是空的(即没有要迭代的元素)。 – crashmstr
@crashmstr我们假设队列已经为二叉树添加了一个根节点。 – shadoweye14