1

だからここに取引があります。ここにコンテナクラスがあります、

#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);

私のインターネット検索では答えが得られなかったので、今、このグリッチを引き起こしている原因を尋ねています。また、誰かがそれを見る必要がある場合は、ヘッダーを投稿します。また、これはオンラインチュートリアルから変更されたクラスです。

編集:エラーメッセージは、vectorsクラスに組み込まれているassertからのものです(式:vector iterators incompatible)

4

1 に答える 1

0

さて、少し眠った後、私はそれを理解しました。他の人への警告として、問題は、イテレータが定義された後に更新されているオブジェクトの1つが新しい動的オブジェクトを作成し、それを無効にすることから発生しました。コンピューターにアクセスできたら、これを説明するコードを編集します。

于 2012-02-17T13:01:16.917 に答える