I tried your find function and it seemed to work. As an aside, avoid function names that are same as standard library ones:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <memory>
#include <deque>
#include <algorithm>//edit: std::move is defined in header <utility>
using namespace std;//to be avoided as well
struct coords
{
int x;
int y;
coords (){}
coords (const int& temp_x, const int& temp_y) : x(temp_x), y(temp_y){}
};
void Raw(coords* t)
{
cout << t->x << " " << t->y << endl;
}
bool find(coords* t, const deque<unique_ptr<coords>>& openList)
{
for (auto p = openList.begin(); p != openList.end(); p++)
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (t->x == (*p)->x && t->y == (*p)->y) //Error is on this line apperently
////////////////////////////////////////////////////////////////////////////////////////////////////////
{
cout << "The coords searched for are in the open list" << endl;
return true;
}
}
return false;
}
void bfs() /////Breadth-First Search
{
deque <unique_ptr < coords > > openList;
deque <unique_ptr < coords > > closedList;
unique_ptr <coords> start(new coords);
unique_ptr <coords> end(new coords);
start->x = 4;
start->y = 4;
end -> x = 8;
end -> y = 8;
openList.push_back(move(start));//The first element of the OPenlist
unique_ptr < coords > current(new coords);
unique_ptr <coords> next(new coords);
while (current != end)
{
// openList.pop_front();
current = move(openList.front());
if (openList.empty() )
{
cout << "Empty" << endl;
//break;
}
if (current == end)
{
cout << "Success" << endl;
break;
}
//NORTH//////////////////////////////////
next->x = current->x;
next->y = current->y+1;
if (find(next.get(), openList) == true || find(next.get(), closedList) == true)
{
closedList.push_back(move(current));
openList.push_back(move(next));
}
}
}
int main()
{
coords* t = new coords(1,2);
unique_ptr<coords> s1 (new coords(3, 4));
unique_ptr<coords> s2 (new coords(1, 2));
deque<unique_ptr<coords>> coords_ptr;
coords_ptr.push_back(std::move(s1));
coords_ptr.push_back(std::move(s2));
find (t, coords_ptr);
delete t;
}
/////////////////////////////////////////
|
The function bfs() is poorly constructed, apart from lots of magic numbers let's go through the logic:
line 49: first element of openList is unique_ptr<coords> start which is moved to unique_ptr<coords> current on line 56. As Peter points out this does not actually remove start from openList but sets it == nullptr. So the following condition if(openList.empty()), line 59, will always be false.
Then the expression on line 64, if (current == end) will never be true because current points to (4, 4) (from start) and end points to (8, 8) (from lines 46, 47).
Line 72, both expressions of the or statement will always be false since (a) openList now just has the nullptr and (b) closedList doesn't have anything! Instead of local variables openList, closedList you might have to pass these deque<unique_ptr<coords>> to the function as parameters