1

I am getting this error on runtime Expression: List iterator not dereferencable. below is the code I am using.

header file

//-----------------------------------------------------------------------------
//
//  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++ file

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

any help in telling me how to fix this would be appreciated Thanking You

Also what does the error mean exactly and how/when does it occur

1
  • Is there ever a time where GetListOfRecentlySensedOpponents() would return an empty list? And is Update() ever called during one of those times? Apr 11, 2013 at 17:37

2 Answers 2

0

You're initialising curBot here:

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

But you don't appear to call Update() anywhere, so on this line:

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

You're attempting to use an uninitialised iterator, which is not dereferencable, as the error message indicates.

The solution: initialise curBot before you use it.

Side note: why is curBot declared at global scope? It would make more sense to declare/initialise it where you actually use it i.e. at the for loop.

8
  • Without seeing the main function, how can you determine that he never calls Update()? Apr 11, 2013 at 17:38
  • @BenjaminLindley That's why I said "You don't appear" - it's an assumption based on an incomplete code sample. It's the OP's responsibility to supply a SSCCE, not mine to second guess whether he has or not.
    – JBentley
    Apr 11, 2013 at 17:40
  • there is about 60 or 70 classes and there is an update function Expression List iterators incompatible i get this error now Apr 11, 2013 at 17:45
  • @AlanFletcher Could you rephrase that? I'm not sure what you mean. If you've solved the first problem and now have a different error message, please post a separate question for the new issue. Also, copy+pasting the exact error message in it's entirety is usually more helpful rather than trying to summarise it in your own words.
    – JBentley
    Apr 11, 2013 at 17:47
  • for (curBot; curBot != SensedBots.end(); ++curBot) when I use this it gives this error Expression List iterators incompatible i dont think its fixed Apr 11, 2013 at 17:48
0

You nitialize the global:

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

only inside Update() with:

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

It is posible that you change SensedBots in other place (for example to other getCurrentOwner()->GetSensoryMem()->GetListOfRecentlySensedOpponents();) and dont set curBot, invalidating the previus value.

Typically you do:

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

Even if you call Update() making curBot=SensedBots.begin(); it is possible that you call pickTarget() 2 time, and SensedBots changed in size in betwen, invalidating the endlich first-call-value of curBot= SensedBots.end(); entering the second time into the for cycle with a "falch" value.

5
  • but i did initialize it curBot = SensedBots.begin(); here Apr 11, 2013 at 18:04
  • And you allways initialize it immediatly before the call of pickTarget ??
    – qPCR4vir
    Apr 11, 2013 at 18:22
  • IF you change SensedBots you very posible invalidate curBot if not reinizialize it.
    – qPCR4vir
    Apr 11, 2013 at 18:51
  • how would i reninitialize it Apr 11, 2013 at 18:59
  • Why not just: for (curBot=SensedBots.begin(); curBot != SensedBots.end(); ++curBot) ?
    – qPCR4vir
    Apr 11, 2013 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.