簡體   English   中英

列表迭代器的運行時間不可取

[英]List iterator not dereferencable run time

我在運行時表達式上遇到此錯誤:列表迭代器不可解除。 下面是我正在使用的代碼。

頭文件

//-----------------------------------------------------------------------------
//
//  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;
}

告訴我如何解決此問題的任何幫助將不勝感激

該錯誤的確切含義是什么,以及如何/何時發生

您要在此處初始化curBot

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

但是您似乎沒有在任何地方調用Update() ,因此在此行上:

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

您正在嘗試使用未初始化的迭代器,該迭代器不可取消引用,如錯誤消息所示。

解決方案:在使用curBot之前curBot進行初始化。

旁注:為什么在全局范圍內聲明curBot 在實際使用它的地方(即在for循環處)聲明/初始化它會更有意義。

您將全局化:

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

僅在Update()中具有:

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

可以在其他位置更改SensedBots (例如,更改為其他getCurrentOwner()->GetSensoryMem()->GetListOfRecentlySensedOpponents();)curBot設置curBot ,從而使之前的值無效。

通常,您會:

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

即使您調用Update()使curBot=SensedBots.begin(); 您可能會pickTarget()調用pickTarget() ,並且SensedBots的大小在betwen中更改,從而使curBot= SensedBots.end();的endlich初次調用值無效curBot= SensedBots.end(); 使用“ falch”值第二次進入for循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM