如何输出包含结构位置的集合
How to output a set containing a Struct Location for
这是基本 A* 算法的开始,如何输出集合中的内容?到目前为止我所拥有的不起作用,我收到此错误
"Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Location' (or there is no acceptable conversion)"
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;
}
//}
}
"Error 1 error C2679: binary '<<' : no operator found which takes a
right-hand operand of type 'const Location' (or there is no acceptable
conversion)"
如果您仔细阅读错误,您会发现您正试图将类型为 'const Location' 的对象作为参数传递给您在 for 循环中执行的提取运算符。
for (it = myset.begin(); it != myset.end(); ++it)
{
cout << ' ' << *it;
}
问题是编译器找不到任何接受该类型的重载(或可接受的转换)。这意味着你肯定没有定义它。
解决方案。
解决方案很简单,您必须为 << 运算符定义适当的重载。以下示例是您可以执行此操作的多种方法之一。
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;
}
};
这是基本 A* 算法的开始,如何输出集合中的内容?到目前为止我所拥有的不起作用,我收到此错误
"Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Location' (or there is no acceptable conversion)"
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;
}
//}
}
"Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Location' (or there is no acceptable conversion)"
如果您仔细阅读错误,您会发现您正试图将类型为 'const Location' 的对象作为参数传递给您在 for 循环中执行的提取运算符。
for (it = myset.begin(); it != myset.end(); ++it)
{
cout << ' ' << *it;
}
问题是编译器找不到任何接受该类型的重载(或可接受的转换)。这意味着你肯定没有定义它。
解决方案。
解决方案很简单,您必须为 << 运算符定义适当的重载。以下示例是您可以执行此操作的多种方法之一。
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;
}
};