• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# std::vector issues

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Captacha  Members

Posted 12 February 2013 - 11:57 AM

In the last week or so, I've been attempting to implement pathfinding for my first time. I've been reading a tutorial on the general concept and have been trying to implement it on my own. For the Closed List and Open List, I use std::vector. The guide says once I find the optimal node to delete it from the Open List and add it to the Closed List. I'm doing that with this code:

closedList.push_back(openList[lowF]);
openList.erase(openList.begin()+
lowF);

The issue is that the wrong node is being deleted from the Open List, causing the pathfinding calculations to flow in an infinite loop. For Instance:

Closed List = (9,4)

Open List = (9,3) (10,4)

After the code I posted above executes, the vectors look like this:

Closed List = (9,4) (9,3)

Open List = (9,3)

While they should look like this

Closed List = (9,4) (9,3)

Open List = (10,4)

I checked the lowF value, and it is 0. So openList.erase(openList.begin()+lowF) should be the same as openList.erase(openList.begin()). For some reason, that line of code is deleting the second element in the vector instead of the first. I'm attaching a error log file I printed out. I'd appreciate any help. ty

### #2frob  Moderators

Posted 12 February 2013 - 12:18 PM

From your log file, it is removing the last element (not second element) of your open list regardless of your other settings.

Will you post the surrounding code of those two push_back() and erase() lines?

Edited by frob, 12 February 2013 - 12:20 PM.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #3Captacha  Members

Posted 12 February 2013 - 01:07 PM

int lowF = 0;
Node tmp;
Node currentNode(Position(currentPos.x, currentPos.y), 0);
vector<Node> openList;
vector<Node> closedList;
closedList.push_back(currentNode);

while(closedList[closedList.size()-1].pos.x != pathDest.x && closedList[closedList.size()-1].pos.y != pathDest.y)
{
getChildNodes(closedList[closedList.size()-1], closedList, openList, wallCords);

for(int i=0;i<openList.size();i++)
{
if(openList[lowF].getF(pathDest) > openList[i].getF(pathDest))
lowF = i;
}
closedList.push_back(openList[lowF]);
openList.erase(openList.begin()+(lowF));
}

path.push_front(closedList[closedList.size()-1]);
tmp = closedList[closedList.size()-1];

while(!(tmp == currentNode))
{
tmp = *tmp.parent;
path.push_front(tmp);
}
path.push_front(tmp);

Here's the entire function. My node class contains a Node* parent, int g, and a Position class which has x and y integers

### #4frob  Moderators

Posted 12 February 2013 - 02:03 PM

Is the function getChildNodes() changing the open list?

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #5Captacha  Members

Posted 12 February 2013 - 07:25 PM

Yes, it does. openList is passed as vector<Node>& openList. Here's the general algorithm for it. The functions in the if statement's body don't affect either list.

Node* tmp;
if(!hasWall(parentNode.pos.x, parentNode.pos.y-1, wallCords) && !onClosedList(Position(parentNode.pos.x, parentNode.pos.y-1), closedList))
{
tmp = new Node(Position(parentNode.pos.x, parentNode.pos.y-1), &parentNode);
openList.push_back(*tmp);
}

Edited by Captacha, 12 February 2013 - 07:27 PM.

Posted 13 February 2013 - 09:10 AM

Is there a copy constructor on the Nodes?  Not sure what would cause this but that's the only thing I can think of, everything else looks pretty typical.

Edit:

Check assignment constructor also.  Failing that you could just break on it in the debugger and just keep clicking "step into" through all the compiler mumbo jumbo to see if anything comes up.

Edit #2 (for reals this time) :

It's an assignment operator, not a constructor.   I think that's what's broken, there's a typo in there that's keeping stuff from getting moved over.  vector.erase(iter) moves everything after iter over, and iter is basically just a pointer.  If this is no good, you can use a vector of pointers to Nodes, or a std::list of nodes (I'm not sure that's guaranteed not to copy on erase, but I have never seen it do it).

Edited by DaDweeDow, 13 February 2013 - 09:44 AM.

### #7frob  Moderators

Posted 13 February 2013 - 11:23 AM

This is basically at the point where you'll need to step through every line with a debugger.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #8Captacha  Members

Posted 13 February 2013 - 12:31 PM

This is the first time I've actually ever used operator overloading before. I didn't really kow what I was doing. Here's the function:

Node Node::operator=(Node copy)
{
return copy;
}

### #9Captacha  Members

Posted 13 February 2013 - 12:59 PM

It seems like that operator= isn't working, so I'll fix it and try to get it to work.

Posted 13 February 2013 - 02:09 PM

yeah that's a no op assignment.  So node1 = node2 doesn't do anything.  Not sure a memcpy(this, &copy, sizeof(Node)) is legal, probably better to just assign all the fields.  You don't need a deep copy though, at least not for this purpose, you could just copy the pointers, since the old copy is going to be dead in a nanosecond.

Edit:

whoops, I realized that was bad advice.  If the destructor of frees an allocated resource, you are going to be dereferencing an invalid pointer (probably, unless that memory gets reused) when you later try to use the copy.  Sorry about that, hope it didn't cause any problems.

Edited by DaDweeDow, 13 February 2013 - 03:53 PM.

### #11Captacha  Members

Posted 14 February 2013 - 08:13 AM

So I fixed my operator=, which fixed the rest. Here's my new function:

Node Node::operator=(Node copy)
{
this->pos.x = copy.pos.x;
this->pos.y = copy.pos.y;
this->parent = copy.parent;
this->g = copy.g;
return *this;
}

Thanks for all the help

### #12EWClay  Members

Posted 14 February 2013 - 09:26 AM

It should return a Node reference, to avoid mistakenly modifying a temporary if you use the return value. Also, the parameter should be a const reference to prevent an unnecessary copy.

And I suspect that if you did not define the = operator, the compiler generated one would do the right thing.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.