我正试图在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;
}https://stackoverflow.com/questions/49301968
复制相似问题