This is the start of a basic A* algorithm, how do I output whats inside the set? What i have so far doesn't work and I get this error
"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)"
If you read the error carefully you will see that you are trying to pass an object of type 'const Location' as a parameter to the extraction operator which you do inside your for loop.
for (it = myset.begin(); it != myset.end(); ++it)
{
cout << ' ' << *it;
}
The problem is that the compiler cannot find any overload (or acceptable conversion) that accepts that type. This means that you most definitely have not defined it.
Solution.
The solution is simple, you must define an appropriate overload for the << operator. The following example is one of many ways you can do this.
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;
}
};