本文介绍了调试断言错误-列表迭代器不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在开发一个程序,该程序应该将每个Window放入列表中,调整其大小,然后根据指定的布局将其移动到屏幕位置.

I'm working on a program that is supposed to put every Window in a list, resize it, and move it to a screen position according to a specified layout.

但是,当我运行此函数时,出现调试断言错误,提示列表迭代器不兼容".

When I'm running this function however I get a debug assertion error saying "list iterators incompatible".

这是代码:

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循环中,希望有人可以帮助我解决此问题

The error occurs during the first for loop, hope someone can help me fix this

这是getWindowList函数:

Here is the getWindowList function:

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

和windowList定义:

and the windowList definition:

std::list <Window> windowList;

推荐答案

您的循环如下所示:

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

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

Given the above, the issue is this:

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

您要返回列表的副本,而不是原始副本.因此,副本的迭代器将在循环中使用,而不是windowList的迭代器.实际上,您在循环结构中使用了两个不同的迭代器,它们中的任何一个都不引用原始列表,仅引用副本.

You're returning a copy of the list, not the original. Thus the iterator to the copy will be used in the loop and not the iterator of windowList. In fact, you are using two different iterators in the loop construct, and neither one of them refers to the original list, only copies.

解决方法是返回引用:

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

您现在要返回对实际列表的引用,而不是副本.现在,您在循环约束中使用的迭代器引用的是同一列表,而不是不同的列表.

You're now returning a reference to the actual list, not a copy. Now the iterators you are using in the loop constraints refer to the same list, not different lists.

这篇关于调试断言错误-列表迭代器不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:11