如何输出包含的结构位置的集合

How to output a set containing a Struct Location for

本文关键字:结构 位置 集合 包含 何输出 输出      更新时间:2023-10-16

这是一个基本a*算法的开始,我如何输出集合中的内容?到目前为止,我所拥有的不起作用,我得到了这个错误

"错误1错误C2679:二进制'<<':找不到接受'const-Location'类型右侧操作数的运算符(或者没有可接受的转换)"

void astar(Location Start_Location, Location End_Location)
{
    Location Current_Location;
    Current_Location.g = 1;
    Start_Location.g = 1;
    int position = 0;
    bool found = false;
    Start_Location.g = 0;
    Start_Location.h = (abs(Start.x - End.x) + abs(Start.y - End.y));
    Start_Location.f = Start_Location.g + Start_Location.h;
    std::set<Location> myset;
    std::set<Location>::iterator it;
    myset.insert(it, Start_Location);
    current_Coord.x = End.x;
    current_Coord.y = End.y;
    /*while (!myset.empty())
    {*/
        Current_Location.h = (abs(current_Coord.x - End.x) + abs(current_Coord.y - End.y));
        Current_Location.f = Current_Location.g + Current_Location.h;
        //calculates f around current node
            Current_Location.h = (abs(current_Coord.x - End.x) + abs((current_Coord.y - 1) - End.y));
            Current_Location.f = Current_Location.g + Current_Location.h;
            myset.insert(it, Current_Location);
            Current_Location.h = (abs(current_Coord.x - End.x) + abs((current_Coord.y + 1) - End.y));
            Current_Location.f = Current_Location.g + Current_Location.h;
            myset.insert(it, Current_Location);
            Current_Location.h = (abs((current_Coord.x - 1) - End.x) + abs(current_Coord.y - End.y));
            Current_Location.f = Current_Location.g + Current_Location.h;
            myset.insert(it, Current_Location);
            Current_Location.h = (abs((current_Coord.x + 1) - End.x) + abs(current_Coord.y - End.y));
            Current_Location.f = Current_Location.g + Current_Location.h;
            myset.insert(it, Current_Location);

            for (it = myset.begin(); it != myset.end(); ++it)
            {
                cout << ' ' << *it;
            }
    //}
}

"错误1错误C2679:二进制'<<':找不到接受"const Location"类型的右侧操作数(或不存在可接受的转换)"

如果您仔细阅读错误,您会发现您正试图将"const-Location"类型的对象作为参数传递给提取运算符,这是您在for循环中执行的操作。

for (it = myset.begin(); it != myset.end(); ++it)
{
    cout << ' ' << *it;
}

问题是编译器找不到任何接受该类型的重载(或可接受的转换)。这意味着你肯定还没有定义它

解决方案

解决方案很简单,必须为<lt;操作人员下面的例子是实现这一点的多种方法之一。

struct Location
{
    int f, g, h;
    friend std::ostream& operator<<(std::ostream& os, const Location& l)
    {
        os << l.f << ' ' << l.g << ' ' << l.h;
        return os;
    }
};