如何输出包含的结构位置的集合
How to output a set containing a Struct Location for
这是一个基本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;
}
};
相关文章:
- 如何循环打印顶点结构
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 编译时检查以确保结构中的任何位置都没有填充
- POD 结构(相同类型的成员):成员是否位于连续的内存位置?
- 如何写入为结构保留的内存位置
- 如何修复结构播放器对象中的矢量位置,以便它在 Main 中与 me.position 一起使用?
- C 中的内存模型:为什么在同一内存位置分配了结构中的两个整数
- 位置结构的奇怪行为
- 在数组中创建位置层次结构
- 尝试创建结构对象,在位置0x3FE00000中创建访问冲突写入错误
- 在任意位置进行快速查找和删除的Deque样数据结构
- 结构中的C++数组指针保留内存位置,但丢失所有值
- gcc 的 std::bind 在源中的哪个位置将参数复制到数据结构中?
- 读取位置/使用结构的矢量时发生未处理的异常访问冲突
- 如何输出包含的结构位置的集合
- 在具有结构的向量中获取项目位置
- 堆栈分配结构总是在同一位置
- 这个结构体的数据在内存中的位置
- 从搅拌机的特定内存位置读取数据(结构)
- 如何为分层结构确定某些东西存储在内存中的位置