Итератор списка, не разыменовываемый во время выполнения

Я получаю эту ошибку во время выполнения выражения: список итераторов не разыменовывается. ниже код, который я использую.

заголовочный файл

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

любая помощь в сообщении мне, как это исправить, будет оценена
Благодарю вас

Также, что конкретно означает ошибка и как / когда она возникает

1

Решение

Вы инициализируете curBot Вот:

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

Но вы, кажется, не звоните Update() в любом месте, поэтому по этой строке:

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

Вы пытаетесь использовать неинициализированный итератор, который не разыменовывается, как указывает сообщение об ошибке.

Решение: инициализация curBot прежде чем использовать его.

Примечание: почему curBot объявлено в глобальном масштабе? Было бы более разумно объявить / инициализировать его там, где вы фактически используете его, т.е. for петля.

0

Другие решения

Вы нитализируете глобальное:

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

только внутри Update () с:

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

Возможно, что вы меняете SensedBots в другом месте (например, в другой getCurrentOwner()->GetSensoryMem()->GetListOfRecentlySensedOpponents();) и не устанавливать curBot, аннулируя предыдущее значение.

Обычно вы делаете:

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

Даже если ты позвонишь Update() изготовление curBot=SensedBots.begin(); возможно, что вы звоните pickTarget() 2 раза и SensedBots изменился в размере между, отменяя значение первого вызова endlich curBot= SensedBots.end(); ввод второй раз в цикл for со значением «falch».

0

По вопросам рекламы [email protected]