我正试图在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_;
}谢谢您抽时间见我。
发布于 2018-03-15 14:35:16
许多构造函数( std)需要比较操作符才能工作。
您使用std::set,它不知道如何比较两个Node对象。
正如在http://en.cppreference.com/w/cpp/container/set中所说的
set是一个关联容器,它包含一组类型为Key的唯一对象的排序集合。排序是使用键比较函数进行比较。
因此,您需要定义比较运算符,或者给std::set一个比较函子作为参数。
编译器告诉您第一个缺失:"<“
struct Node {
friend bool operator< (const Node& _nLeft, const Node& _nRight);
//friend not necessary since we use struct (full public)
...
};
bool operator< (const Node& _nLeft, const Node& _nRight)
{
if (_nLeft.type < _nRight.type)
return true;
...
return false;
}发布于 2018-03-15 14:30:03
在没有重载operator<或定义您自己的自定义比较器的情况下,您不能拥有std::设置。set通常是一棵红色-黑色的树,对象是键,这需要能够比较键。
因此,您可以为节点创建一个operator<,也可以创建一个自定义比较器。有关自定义比较器这里的信息。
发布于 2018-03-15 14:31:09
set是一个关联容器,它包含一组类型为Key的唯一对象的排序集合。排序是使用键比较函数进行比较。
来源
您必须重载节点的operator<。
https://stackoverflow.com/questions/49301968
复制相似问题