C++ 为什么向量迭代器不兼容?
我正在执行以下操作并获得该调试错误:C++ 为什么向量迭代器不兼容?,c++,vector,C++,Vector,我正在执行以下操作并获得该调试错误: AguiWidgetBase* AguiWidgetContainer::recursiveGetWidgetUnderMouse( AguiWidgetBase* root, const AguiMouseEventArgs &mouse) { AguiWidgetBase* currentNode = root; bool foundsomething = true; while(foundsomething)
AguiWidgetBase* AguiWidgetContainer::recursiveGetWidgetUnderMouse(
AguiWidgetBase* root, const AguiMouseEventArgs &mouse)
{
AguiWidgetBase* currentNode = root;
bool foundsomething = true;
while(foundsomething)
{
foundsomething = false;
if(currentNode->getChildCntrolCount() > 0)
for (std::vector<AguiWidgetBase*>::const_reverse_iterator rit =
currentNode->getChildRBeginIterator();
rit < currentNode->getChildREndIterator(); ++rit)
{
if(!foundsomething)
if ((*rit)->intersectionWithPoint(mouse.getPosition()))
{
foundsomething = true;
currentNode = *rit;
}
}
}
return currentNode;
}
// ...
AguiWidgetBase*AguiWidgetContainer::recursiveGetWidgetUnderMouse(
AguiWidgetBase*根,常量AguiMouseEventArgs和鼠标)
{
AguiWidgetBase*currentNode=root;
bool foundsomething=true;
当(发现某事)
{
发现某事=错误;
如果(currentNode->getChildCntrolCount()>0)
对于(std::vector::const\u reverse\u迭代器rit)=
currentNode->getChildRBeginIterator();
ritgetChildREndIterator();++rit)
{
如果(!找到某物)
if((*rit)->intersectionWithPoint(mouse.getPosition())
{
发现某事=真实;
currentNode=*rit;
}
}
}
返回当前节点;
}
// ...
当currentNode成为指向root的子级的指针并在for上崩溃后,它将失败
我做错了什么
谢谢您正在重新分配结束位置,迭代器正在检查该位置 2评论:
if
语句周围。您可以不这样做,但这样做会使代码更可读,并提醒您缩进if
中添加一个break语句,这样就不需要继续遍历向量。不需要额外的变量和变量检查,这将完全实现您想要的@米洛-见我的编辑。在内部if语句中添加break语句,以便使for循环短路。但是,首先不要重新分配迭代器。如果您觉得必须,请使用不同的变量!