c++ - 自定义结构错误 : "VS 2015 error C2678: binary ' <': no operator found which takes a left-hand operand of type ' const Node'"

标签 c++ visual-studio-2015 path-finding a-star stdset

我正在尝试在 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/

相关文章:

machine-learning - 如何用神经网络实现寻路和拥塞检测?

c++ - 如何找出键盘的地址

c++ - Visual Studio 不允许我使用 sqrt 或 floor,对重载函数的调用不明确

c++ - 在这种情况下,可以避免在c++中手动管理内存吗?

mysql - VS 中无法连接 MySQL 到 EF6

xslt - Visual Studio 2015 社区 - 缺少 "Show XSL Output"选项

mvvm - 如何在 Windows Universal App 10 的页面资源中将 DataTemplate 设置为 ContentControl?

java - Dijkstra 算法给出错误的最短路径

algorithm - 基于AI招式的提示功能

java.lang.ClassNotFoundException : Didn't find class on path: dexpathlist