1

我在运行时表达式上收到此错误:列表迭代器不可取消引用。下面是我正在使用的代码。

头文件

//-----------------------------------------------------------------------------
//
//  Name:   Fletcher_TargetingSystem.h
//
//  Author: Alan Fletcher (20040797)
//
//  Desc:   class to select a target from the opponents currently in a bot's
//          perceptive memory.
//-----------------------------------------------------------------------------
#include "2d/Vector2D.h"
#include <list>
#include "../../AbstTargetingSystem.h"


class AbstRaven_Bot;

class Fletcher_TargetingSystem : public AbstTargetingSystem
{

public:
    Fletcher_TargetingSystem(AbstRaven_Bot* owner);

    //each time this method is called the opponents in the owner's sensory 
    //memory are examined and the closest  is assigned to m_pCurrentTarget.
    //if there are no opponents that have had their memory records updated
    //within the memory span of the owner then the current target is set
    //to null

    void       Update();
    void         closestBotStrategy();

    void      setCurrentTarget(AbstRaven_Bot* t);
    AbstRaven_Bot* getCurrentTarget();

    void      setCurrentOwner(AbstRaven_Bot* t);
    AbstRaven_Bot* getCurrentOwner();
};

class Fletcher_getClosestBotStrategy
{

public:
    Fletcher_getClosestBotStrategy()
    {}
    Fletcher_getClosestBotStrategy(AbstRaven_Bot* owner);

    //each time this method is called the opponents in the owner's sensory 
    //memory are examined and the closest  is assigned to m_pCurrentTarget.
    //if there are no opponents that have had their memory records updated
    //within the memory span of the owner then the current target is set
    //to null
    void      pickTarget(AbstRaven_Bot& owner);
};

#endif

c++文件

#include "Fletcher_TargetingSystem.h"
#include "../../AbstRaven_Bot.h"
#include "../../Raven_SensoryMemory.h"
#include "../../Debug/DebugConsole.h"


//-------------------------------- ctor ---------------------------------------
//-----------------------------------------------------------------------------
Fletcher_TargetingSystem::Fletcher_TargetingSystem(AbstRaven_Bot* owner):
AbstTargetingSystem(owner){

}

//-------------------------------- ctor ---------------------------------------
//-----------------------------------------------------------------------------
Fletcher_getClosestBotStrategy::Fletcher_getClosestBotStrategy(AbstRaven_Bot* owner){

}
std::list<AbstRaven_Bot*> SensedBots;
std::list<AbstRaven_Bot*>::const_iterator curBot;
//----------------------------- Update ----------------------------------------

//-----------------------------------------------------------------------------
//----------------------------- closestPlayer ----------------------------------------

//-----------------------------------------------------------------------------
void Fletcher_getClosestBotStrategy::pickTarget(AbstRaven_Bot& owner)
{

    double ClosestDistSoFar = MaxDouble;

    Fletcher_TargetingSystem *fTS = new Fletcher_TargetingSystem(&owner);


    for (curBot; curBot != SensedBots.end(); ++curBot)
    {
        //make sure the bot is alive and that it is not the owner
        if ((*curBot)->isAlive() && (*curBot != fTS->getCurrentOwner()) )
        {
            double dist = Vec2DDistanceSq((*curBot)->Pos(), fTS->getCurrentOwner()->Pos());
            if (dist < ClosestDistSoFar)
            {
                ClosestDistSoFar = dist;
                fTS->setCurrentTarget(*curBot);// = *curBot;
            }

        }
    }
    //return *fTS;
}

void Fletcher_TargetingSystem::Update()
{
    // currentStrategy = targetClosestBotStrategy;
    // target = currentStrategy.pickTarget();
    //std::list<AbstRaven_Bot*> SensedBots;
    SensedBots = getCurrentOwner()->GetSensoryMem()->GetListOfRecentlySensedOpponents();
    curBot = SensedBots.begin();
    //std::list<AbstRaven_Bot*>::const_iterator curBot = SensedBots.begin();
    setCurrentTarget(0);//       = 0;

    Fletcher_getClosestBotStrategy* fGCBS = new Fletcher_getClosestBotStrategy(this->getCurrentOwner());
    fGCBS->pickTarget(**curBot);
}

AbstRaven_Bot* Fletcher_TargetingSystem::getCurrentOwner(){
    return m_pOwner;
}
AbstRaven_Bot* Fletcher_TargetingSystem::getCurrentTarget(){
    return m_pCurrentTarget;
}

void Fletcher_TargetingSystem::setCurrentTarget(AbstRaven_Bot* t){
    m_pCurrentTarget = t;
}
void Fletcher_TargetingSystem::setCurrentOwner(AbstRaven_Bot* t){
    m_pOwner = m_pOwner;
}

任何帮助告诉我如何解决这个问题将不胜感激谢谢

还有错误的确切含义是什么以及它是如何/何时发生的

4

2 回答 2

0

你在curBot这里初始化:

void Fletcher_TargetingSystem::Update()
{
    // ...
    curBot = SensedBots.begin();
    // ...
}

但你似乎没有在Update()任何地方打电话,所以在这一行:

for (curBot; curBot != SensedBots.end(); ++curBot)
{ // ... }

如错误消息所示,您正在尝试使用不可取消引用的未初始化迭代器。

解决方案:使用前先初始化curBot

旁注:为什么curBot在全局范围内声明?for在您实际使用它的地方(即在循环中)声明/初始化它会更有意义。

于 2013-04-11T17:36:41.023 回答
0

你初始化全局:

std::list<AbstRaven_Bot*>::const_iterator curBot;

仅在 Update() 内部具有:

SensedBots = getCurrentOwner()->GetSensoryMem()
              ->GetListOfRecentlySensedOpponents();
curBot = SensedBots.begin();

您可能会SensedBots在其他地方进行更改(例如更改为 othergetCurrentOwner()->GetSensoryMem()->GetListOfRecentlySensedOpponents();)并且不设置curBot,从而使以前的值无效。

通常你会:

for (curBot=SensedBots.begin();  curBot != SensedBots.end(); ++curBot)

即使您调用Update()makecurBot=SensedBots.begin(); 也有可能调用pickTarget()2 次,并且在两者之间更改大小,从而使第二次进入 for 循环SensedBots的 endlich first-call-value 无效, 并带有“falch”值。curBot= SensedBots.end();

于 2013-04-11T17:38:46.097 回答