C++ 如何访问超出范围的变量? class-foo{ 向量*队列; 向量位置; foo(向量*队列,向量pos){ 这个->队列=队列; 这个->位置=位置; } 公众: foo(向量*队列){ 这个->队列=队列; } void init(){ 向量posNew=pos; //创建状态FOO的二叉树子级 posNew.push_back(/*一个附加值*/) foo newFoo(队列,posNew); 队列->推回(&newFoo); }//这里的变量newFoo和posNew超出范围,因此它们甚至从队列中删除 } 分类栏{ vector queue;//假设队列中添加了根节点。 bar(){ for(unsigned int i=0;iinit();//在计算第三个元素时,由于我假设对象被删除,所以值溢出 } } }
我正在尝试使用BFS搜索和队列来解决问题。但是,由于我创建的对象子对象超出范围,因此我无法使队列正常工作。对此的任何帮助都将不胜感激 编辑: 在我的实际代码中,我遇到了问题,因为当对象超出范围时,它会向我显示这些内存分配。C++ 如何访问超出范围的变量? class-foo{ 向量*队列; 向量位置; foo(向量*队列,向量pos){ 这个->队列=队列; 这个->位置=位置; } 公众: foo(向量*队列){ 这个->队列=队列; } void init(){ 向量posNew=pos; //创建状态FOO的二叉树子级 posNew.push_back(/*一个附加值*/) foo newFoo(队列,posNew); 队列->推回(&newFoo); }//这里的变量newFoo和posNew超出范围,因此它们甚至从队列中删除 } 分类栏{ vector queue;//假设队列中添加了根节点。 bar(){ for(unsigned int i=0;iinit();//在计算第三个元素时,由于我假设对象被删除,所以值溢出 } } },c++,pointers,C++,Pointers,我正在尝试使用BFS搜索和队列来解决问题。但是,由于我创建的对象子对象超出范围,因此我无法使队列正常工作。对此的任何帮助都将不胜感激 编辑: 在我的实际代码中,我遇到了问题,因为当对象超出范围时,它会向我显示这些内存分配。 这个绿色部分是根节点所在的位置,红色部分是子节点的预期数据所在的位置,但它现在已被删除。变量queue是foo指针的向量,而不是foo对象。但是在init(),您将newFoo声明为foo对象并将其推入队列。newFoo是函数init()的局部变量,因此当函数完成执行时,n
这个绿色部分是根节点所在的位置,红色部分是子节点的预期数据所在的位置,但它现在已被删除。变量
queue
是foo
指针的向量,而不是foo
对象。但是在init()
,您将newFoo
声明为foo
对象并将其推入队列。newFoo
是函数init()
的局部变量,因此当函数完成执行时,newFoo
将丢失
您可以将newFoo
声明为指针并为其分配内存,如
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
}
}
}
然后将
newFoo
推到队列中。变量queue
是foo
指针的向量,而不是foo
对象。但是在init()
中,您将newFoo
声明为foo
对象并将其推到队列中。newFoo
是函数init()的局部变量
,因此当函数完成执行时,newFoo
将丢失
您可以将newFoo
声明为指针并为其分配内存,如
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
}
}
}
并在您的队列中推送newFoo
。“超出范围”有两种含义:
std::unique_ptr
或std::shared_ptr
)管理的动态生存期将是一个不错的选择。有两种含义“超出范围”:
对于您的队列,由智能指针(
std::unique\u ptr
或std::shared\u ptr
)管理的动态生命周期将是一个不错的选择。为什么需要init()
?将它所做的内容移动到ctor(foo(vector*)
)。在栏的构造函数中,队列
将为空(即没有可迭代的元素)。@crashmstr我们假设队列allready有一个根节点添加到二叉树中。@gingerplus-ctor?@shadoweye14构造函数为什么需要init()
?将它所做的事情移动到ctor(foo(vector*)
)。在条
的构造函数中,队列
将为空(即没有可迭代的元素)@crashmstr我们假设队列allready有一个根节点添加到二叉树中。@gingerplus-ctor?@shadoweye14-constructorah我的缺点是,我实际上是使用&newFoo
操作符来做这件事,而不是最初像指针一样声明它。这是一样的吗?不。newFoo
仍然是一个问题局部变量,在init()之后将丢失
完成。所以&newFoo
会指向一些垃圾值。啊!但是在实际代码中,因为我有多个foo*
参数,它在无符号int
参数上给了我这个错误。猜猜为什么?@shadoweye14它不起作用。局部指针变量只是指向内存位置而不是作为对象本身。一个很好的想象方式是,你有一座房子和一个指向房子的标志。当你摧毁房子时,房子就不见了。当你摧毁指向房子的标志时,房子仍然在那里。你在代码中的做法是创建一座房子,建造一个指向房子的标志,然后斯特罗的房子。标志仍然指向房子曾经所在的地方。然而,正如我提到的,如果我在我的真实代码中这样做,我就是ge