2012-02-17 78 views
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); 

我的互联网搜索没有得到答案,所以现在我问你是什么让这个小故障了。如果有人需要查看,我还会发布标题。此外,这是来自在线教程的修改后的课程。

编辑:错误消息是从载体类内置断言(表达式:矢量迭代器不兼容)

+0

你得到的* exact *错误信息是什么? – 2012-02-17 02:18:32

回答

0

好,一些睡眠后我计算出来。作为对其他人的警告,问题来自其中一个被更新的对象,在迭代器被定义之后创建一个新的动态对象,使其失效。我会编辑这个有一段代码解释这个,一旦我可以到电脑。