首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有自定义结构的错误:"VS 2015错误C2678:二进制'<':找不到使用'const‘类型左操作数的操作符。“

带有自定义结构的错误:"VS 2015错误C2678:二进制'<':找不到使用'const‘类型左操作数的操作符。“
EN

Stack Overflow用户
提问于 2018-03-15 14:22:39
回答 3查看 308关注 0票数 2

我正试图在2D网格上创建一个A*算法的实现,并且已经到了我需要创建一组节点的邻居的位置。下面是我正在使用的结构。

代码语言:javascript
运行
复制
// 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;
};

当我在这里创建这个函数时,会出现错误:

代码语言:javascript
运行
复制
// 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_;
}

谢谢您抽时间见我。

EN

Stack Overflow用户

回答已采纳

发布于 2018-03-15 14:35:16

许多构造函数( std)需要比较操作符才能工作。

您使用std::set,它不知道如何比较两个Node对象。

正如在http://en.cppreference.com/w/cpp/container/set中所说的

set是一个关联容器,它包含一组类型为Key的唯一对象的排序集合。排序是使用键比较函数进行比较。

因此,您需要定义比较运算符,或者给std::set一个比较函子作为参数。

编译器告诉您第一个缺失:"<“

代码语言:javascript
运行
复制
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;
}
票数 -1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49301968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档