在c ++中使用重新定义的变量确定范围问题

时间:2013-04-05 05:18:22

标签: c++ visual-studio-2012 scope

给出以下代码:

for (list<Vertex*>::iterator v = vertexList->begin(); v != vertexList->end(); v++)
{
    if (!(*(*v) == *u))
    {
        (*v)->setColor(white);
        (*v)->setPred(NULL);
        (*v)->setDist(INFINITY);
    }
}

queue<Vertex*> Q;
Q.push(u);
Vertex* v = Q.front();  // WONT assign the item that is in the queue to v
Vertex* t = Q.front();        // WILL assign the item that is in the queue to t

正如您所看到的,变量V已在for循环中定义,并且(至少据我所知)应仅在for范围中持续。 (虽然for是有效的。)

但是,当我尝试将Q前端分配给名为V的新变量时,它不会真正将当前位于QUEUE中的项目放入其中。相反,我只是看到??????在里面。 (使用visual STUDIO 2012)。编译成功通过。

另一方面,当我尝试将Q front分配给一个未命名为v的变量时,我会这样做,T将保持正确的值(队列中的第一项)。

我在这里错过了什么..?

1 个答案:

答案 0 :(得分:0)

我认为这只是一个调试器错误,如果有一个描述属性或Vertex的成员,您可以写入控制台进行控制,您将看到正确分配了Vertex * v。

相关问题