gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:36:22 24 4
gpt4 key购买 nike

   #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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com