c++ - '列表迭代器不可取消引用'

标签 c++ list stl

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

我收到运行时错误“list iterator not dereferencable”。

我假设这与从 while 循环中调用 workingset.pop_front() 有关,但我不确定我应该如何补救。

每个循环,我想从列表的前面获取节点,稍微处理一下,然后从列表中删除该节点。

这是 generatelegalmove() 的代码——如您所见,如果新方 block 在棋盘上(即在数组的两个维度上都在 0-9 范围内,并且方 block 为空),它将添加这个工作集的新节点和 board[][](实际上是一个有效的重复列表)

最佳答案

根据您提供的样本,我可以看出一件事是错误的。在每次循环迭代结束时,弹出前端节点。但是,只有当 goal_found 为真时,您才会退出循环,这意味着该行:

square nodetocheck = workingset.front();

... 可以很好地访问一个空的工作集。显然,您在这里调用了可能会添加节点的其他函数,但如果不存在节点,这可能是个问题。

编辑:我相信您的代码没有添加另一个节点,因为您使用的是按位与 & 运算符而不是逻辑与 && 运算符,导致您的工作设置不获取节点。

关于c++ - '列表迭代器不可取消引用',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668381/

相关文章:

python - 在python中打印字典的原始输入顺序

c++ - 设计和编码一个非碎片化的静态内存池

c++ - 如何使用可变参数模板使用 std::function

c++ - 限制文件流的文件大小?

c++ - 为什么 size_type 在 64 位平台上是 32 位整数?

c++ - 具有多个输出的 Typedef

c++ - boolean 类型的递归

c++ - 无需任何直接操作的变量更改

python - Spyder 不在变量资源管理器中显示列表和数组

linq - “等于”和“顺序等于”之间的区别?