令人困惑的向量迭代器不兼容的运行时错误

发布于 2025-01-06 14:42:08 字数 3374 浏览 1 评论 0原文

所以这就是交易。 我这里有一个容器类,

#include "stdafx.h"
#include "ObjectManager.h"
#include "Game.h"

ObjectManager::ObjectManager()
{
}

ObjectManager::~ObjectManager()
{
   std::for_each(_objects.begin(),_objects.end(),ObjectDeallocator());
   std::for_each(_dynamicObjects.begin(),_dynamicObjects.end(),DynamicObjectDeallocator());
}


void ObjectManager::Add(std::string name, VisibleGameObject *object)
{
    _objects.insert(std::pair<std::string, VisibleGameObject*>(name,object));
}

void ObjectManager::Remove(std::string name)
{
    std::map<std::string, VisibleGameObject*>::iterator results = _objects.find(name);
    if (results != _objects.end())
    {
        delete results->second;
        _objects.erase(results);
    }
}

int ObjectManager::GetObjectCount() const
{
    return _objects.size();
}

VisibleGameObject* ObjectManager::Get(std::string name) const
{
    std::map<std::string, VisibleGameObject*>::const_iterator results = _objects.find(name);
    if (results == _objects.end())
        return NULL;
    return results->second;
}


void ObjectManager::AddDynamic(VisibleGameObject *object)
{
    _dynamicObjects.push_back(object);
}

void ObjectManager::PostRemoveDynamic()
{
        std::vector<VisibleGameObject*>::const_iterator iter = _dynamicObjects.begin();
    while(iter != _dynamicObjects.end())
    {
        if ((*iter)->IsDeleted())
        {
            delete (*iter);
            _dynamicObjects.erase(iter);
        }
    iter++;
    }
}

const std::vector<VisibleGameObject*>& ObjectManager::GetDynamicContainer()
{
    return _dynamicObjects;
}


void ObjectManager::DrawAll(sf::RenderWindow& renderWindow)
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _objects.begin();
    while(itr != _objects.end())
    {
        itr->second->Draw(renderWindow);
        itr++;
    }

    std::vector<VisibleGameObject*>::const_iterator iter = _dynamicObjects.begin();
    while(iter != _dynamicObjects.end())
    {
        (*iter)->Draw(renderWindow);
        iter++;
    }
}

void ObjectManager::UpdateAll()
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _objects.begin();
    std::vector<VisibleGameObject*>::iterator iter;
    iter = _dynamicObjects.begin();

    float timeDelta = GameEngine::GetWindow().GetFrameTime();

    while(itr != _objects.end())
    {
        itr->second->Update(timeDelta);
        itr++;
    }

    while(iter != _dynamicObjects.end())
    {
        (*iter)->Update(timeDelta);
        iter++;
    }

}

并且使用断点,我已经确定问题出在这个函数中,

void ObjectManager::UpdateAll()
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _objects.begin();
    std::vector<VisibleGameObject*>::iterator iter;
    iter = _dynamicObjects.begin();

    float timeDelta = GameEngine::GetWindow().GetFrameTime();

    while(itr != _objects.end())
    {
        itr->second->Update(timeDelta);
        itr++;
    }

    while(iter != _dynamicObjects.end())
    {
        (*iter)->Update(timeDelta);
        iter++;
    }

}

具体来说,这一行,

    (*iter)->Update(timeDelta);

我的互联网搜索没有给出答案,所以现在我问你是什么导致了这个故障。如果有人需要的话我也会发布标题。另外,这是在线教程的修改课程。

编辑:错误消息来自向量类中的内置断言。(表达式:向量迭代器不兼容)

So here is the deal.
I have a container class here,

#include "stdafx.h"
#include "ObjectManager.h"
#include "Game.h"

ObjectManager::ObjectManager()
{
}

ObjectManager::~ObjectManager()
{
   std::for_each(_objects.begin(),_objects.end(),ObjectDeallocator());
   std::for_each(_dynamicObjects.begin(),_dynamicObjects.end(),DynamicObjectDeallocator());
}


void ObjectManager::Add(std::string name, VisibleGameObject *object)
{
    _objects.insert(std::pair<std::string, VisibleGameObject*>(name,object));
}

void ObjectManager::Remove(std::string name)
{
    std::map<std::string, VisibleGameObject*>::iterator results = _objects.find(name);
    if (results != _objects.end())
    {
        delete results->second;
        _objects.erase(results);
    }
}

int ObjectManager::GetObjectCount() const
{
    return _objects.size();
}

VisibleGameObject* ObjectManager::Get(std::string name) const
{
    std::map<std::string, VisibleGameObject*>::const_iterator results = _objects.find(name);
    if (results == _objects.end())
        return NULL;
    return results->second;
}


void ObjectManager::AddDynamic(VisibleGameObject *object)
{
    _dynamicObjects.push_back(object);
}

void ObjectManager::PostRemoveDynamic()
{
        std::vector<VisibleGameObject*>::const_iterator iter = _dynamicObjects.begin();
    while(iter != _dynamicObjects.end())
    {
        if ((*iter)->IsDeleted())
        {
            delete (*iter);
            _dynamicObjects.erase(iter);
        }
    iter++;
    }
}

const std::vector<VisibleGameObject*>& ObjectManager::GetDynamicContainer()
{
    return _dynamicObjects;
}


void ObjectManager::DrawAll(sf::RenderWindow& renderWindow)
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _objects.begin();
    while(itr != _objects.end())
    {
        itr->second->Draw(renderWindow);
        itr++;
    }

    std::vector<VisibleGameObject*>::const_iterator iter = _dynamicObjects.begin();
    while(iter != _dynamicObjects.end())
    {
        (*iter)->Draw(renderWindow);
        iter++;
    }
}

void ObjectManager::UpdateAll()
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _objects.begin();
    std::vector<VisibleGameObject*>::iterator iter;
    iter = _dynamicObjects.begin();

    float timeDelta = GameEngine::GetWindow().GetFrameTime();

    while(itr != _objects.end())
    {
        itr->second->Update(timeDelta);
        itr++;
    }

    while(iter != _dynamicObjects.end())
    {
        (*iter)->Update(timeDelta);
        iter++;
    }

}

and, using breakpoints, I have determined the problem is in this function here,

void ObjectManager::UpdateAll()
{
    std::map<std::string, VisibleGameObject*>::const_iterator itr = _objects.begin();
    std::vector<VisibleGameObject*>::iterator iter;
    iter = _dynamicObjects.begin();

    float timeDelta = GameEngine::GetWindow().GetFrameTime();

    while(itr != _objects.end())
    {
        itr->second->Update(timeDelta);
        itr++;
    }

    while(iter != _dynamicObjects.end())
    {
        (*iter)->Update(timeDelta);
        iter++;
    }

}

Specifically, this line,

    (*iter)->Update(timeDelta);

My internet searches haven't yielded the answer, so now I'm asking you what's making this glitch up. Also I'll post the header if anybody needs to see it. Also also, this is a modified class from an online tutorial.

Edit: The error message is from a built-in assert in the vectors class.(Expression: vector iterators incompatible)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的呼吸 2025-01-13 14:42:08

好吧,睡了一觉后我就明白了。作为对其他人的警告,问题来自于定义迭代器后更新的对象之一创建新的动态对象,使其无效。一旦我可以使用计算机,我将编辑一些代码来解释这一点。

Okay, after some sleep I figured it out. As a warning to others, the problem came from one of the objects being updated creating a new dynamic object after the iterator was defined, invalidating it. I will edit this woth some code explaining this once I can get to a computer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文