我正在尝试在 2D 网格上创建 A* 算法的实现,但遇到了需要创建一组节点邻居的问题。以下是我正在使用的结构。
// Holds values for x and y locations on the grid
struct Coord {
int x, y;
};
// holds data for each node required for A*
struct Node {
int type; // used for defining if this node is a blocker, empty, start or end
Coord location;
int g = 0;
int h = 0;
int f = g + h;
Node *parent_; // pointer to this node's parent
std::string debugmessage;
};
当我在这里创建这个函数时出现错误:
// finds a node's neighbours for A*
std::set<Node> neighbours(Node& n_) {
std::set<Node> neighbours_;
Node temp = n_;
int x = temp.location.x;
int y = temp.location.y;
// start at the location belonging to 'n_'
for (y; y < HEIGHT; y++) {
for (x; x < WIDTH; x++) {
// east
if (x < WIDTH - 1) {
neighbours_.insert(astarArray[x + 1][y]);
}
// west
if (x > 0) {
neighbours_.insert(astarArray[x - 1][y]);
}
// south
if (y < HEIGHT - 1) {
neighbours_.insert(astarArray[x][y + 1]);
}
// north
if (y > 0) {
neighbours_.insert(astarArray[x][y -1]);
}
}
}
return neighbours_;
}
感谢您的宝贵时间。
最佳答案
如果不重载运算符<或定义自己的自定义比较器,就无法拥有 std::set 的东西。 std::set 通常是一棵红黑树,其中对象是键,并且需要能够比较键。
因此,要么为节点创建一个运算符<,要么创建一个自定义比较器。有关自定义比较器的信息 here 。
关于c++ - 自定义结构错误 : "VS 2015 error C2678: binary ' <': no operator found which takes a left-hand operand of type ' const Node'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49301968/