gpt4 book ai didi

c++ - 调试断言错误 - 列表迭代器不兼容

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:12 25 4
gpt4 key购买 nike

我正在开发一个程序,该程序应该将每个窗口放在一个列表中,调整它的大小,并根据指定的布局将它移动到屏幕位置。

当我运行这个函数时,我收到一个调试断言错误,提示“列表迭代器不兼容”。

代码如下:

void Control::checkForNewWindows()
{
for (std::list<Window>::iterator i = mainDetector.getWindowList().begin(); i != mainDetector.getWindowList().end(); ++i)
{
bool forBreak = false;
if ((i->getTitle().find("sample_title") != std::string::npos) && (i->getState() == false))
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 4; x++)
{
if (activeLayout.windowLayout[y][x].getHandle() == 0)
{
moveWindow(*i, activeLayout.dimensionsLayout[y][x].x, activeLayout.dimensionsLayout[y][x].y, activeLayout.dimensionsLayout[y][x].width,
activeLayout.dimensionsLayout[y][x].height);
activeLayout.windowLayout[y][x] = *i;
activeLayout.windowLayout[y][x].setState(true);
forBreak = true;
}
if (forBreak)
{
break;
}
}
if (forBreak)
{
break;
}
}
}
}
}

第一次for循环时出现错误,希望有人能帮我解决这个问题

编辑:

这里是 getWindowList 函数:

std::list <Window> Detector::getWindowList()
{
return windowList;
}

和 windowList 定义:

std::list <Window> windowList;

最佳答案

你的循环看起来像这样:

for (std::list<Window>::iterator i = mainDetector.getWindowList().begin(); 
i != mainDetector.getWindowList().end();
++i)

鉴于上述情况,问题是这样的:

std::list <Window> Detector::getWindowList()
{
return windowList;
}

您返回的是列表的拷贝,而不是原件。因此拷贝的迭代器将在循环中使用,而不是 windowList 的迭代器。事实上,您在循环构造中使用了两个不同的迭代器,它们都没有引用原始列表,仅引用了拷贝。

解决方法是返回一个引用:

std::list <Window>& Detector::getWindowList()
{
return windowList;
}

您现在返回的是对实际列表的引用,而不是拷贝。现在,您在循环约束中使用的迭代器引用相同的列表,而不是不同的列表。

关于c++ - 调试断言错误 - 列表迭代器不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com