开发者

'list iterator not dereferencable'

   #include <iostream> 
using namespace std; 

#include <list> 


//A queue for the working set 
//x,y co-ords of the square, path length so far 
struct square {
    int x;
    int y; 
    int path_length;
};

list<square> workingset; 


//A 2D array of ints to represent the board (duplicates list)
int board[10][10]; 

void generatelegalmove(square node, int x_offset, int y_offset); 
void printboard(); 

void main()
{
    //Initialises the board 
    int i, j;
    for (i=0; i<10; i++)
    {
        for (j=0; j<10; j++)
        {
            board[i][j] = 0;
        }
    }

    //The goal position - a number we will never reach 
    board[8][8] = 1337; 

    bool goal_found = false; 

    //Sets up initial position 
    square temp = {3, 7, 1}; 

    //Put initial position in working set and duplicates list
    workingset.push_back(temp); 
    board[3][7] = 1; 

    //Loop (until a goal is found)
    while(!goal_found)
    {
        //Get the head node from the working set
        square nodetocheck = workingset.front();

        //Exit if the goal has been found
        if(board[nodetocheck.x][nodetocheck.y] == 1337)
        {
            goal_found = true;
            break; 
        }

        //Generate the legal moves 
        generatelegalmove(nodetocheck, -1, 0); //One square to the left
        generatelegalmove(nodetocheck, 0, -开发者_开发技巧1); //One square up
        generatelegalmove(nodetocheck, 1, 0); //One square to the right
        generatelegalmove(nodetocheck, 0, 1); //One square down


        if(!workingset.empty())
        {
                //workingset.pop_front();
        }

    //End Loop
    }

    //Print the Board 
    printboard();
    while(true);

    //Trace back and print Trace back (once implemented) 

    //Print other info 

}


void generatelegalmove(square node, int x_offset, int y_offset)
{
    node.x = node.x + x_offset;
    node.y = node.y + y_offset; 
    node.path_length = node.path_length+1; 
    //Is this square on the board
    if((node.x >= 0) && 
       (node.x < 10) && 
       (node.y >= 0) && 
       (node.y < 10) && 
       //Is this square empty
       (board[node.x][node.y] == 0))
    {

        workingset.push_back(node);
        board[node.x][node.y] = node.path_length; 
        //Add to working set
        //Add to duplicates list 
    }
    //(If a graphical animation is added, do it here, by printing the new board after each one, then sleeping for a few seconds) 
} 

I get the runtime error 'list iterator not dereferencable'.

I'm assuming this is to do with workingset.pop_front() being called from within the while loop, but I'm not sure what I should do to remedy this.

Each loop, I want to get the node from the front of the list, work with it a little, then remove that node from the list.

This is the code for generatelegalmove() - as you can see, if the new square is on the board (i.e. within the range of 0-9 in both dimensions of the array, and the square is empty) it will add this new node to the working set and the board[][] (which is effectively a duplicates list in effect)


Given the sample you have provided, I can see one thing wrong. At the end of each loop iteration, you pop the front node. However, you only exit out of your loop if goal_found is true, meaning the line:

square nodetocheck = workingset.front();

... could well access an empty working set. Obviously you have calls to other functions here that might add nodes, but this might be a problem if no nodes existed.

EDIT: I believe your code is not adding another node because you are using the bitwise-and & operator instead of a logical-and && operator, causing your working set not to gain nodes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜