Thread: need help with vectors

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    need help with vectors

    hi,

    I'm having some trouble with a vector. I'm at a point where I need to remove and object from the vector and I inconsistently receive the error:

    "vector iterator not dereferencable"

    Although I'm not sure what the issue is here as I'm not dereferencing the iterator. If I break and debug it goes into the actual vector class and other built in stuff.

    Code:
    for (vector<Building>::iterator itBuilding = buildings.begin(); itBuilding != buildings.end(); itBuilding++) {
    	if (itBuilding->isPointInObject(&downPoint)) {
    		buildings.erase(itBuilding);
    		break;
    	}
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are dereferencing the iterator instead of the Building.
    Code:
    if ((*itBuilding).isPointInObject(&downPoint)) {

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    No, that is legal as roznor did it, the iterator->member is the same as (*iterator).member

    I see one issue, the fact that erase() will invalidate the iterator, but I'm not seeing any other issues. Nothing that will give a compile time error.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see anything wrong with the code, it should be the same as bithub's example. Is this a run-time error? It is probably saying that you are dereferencing an invalid iterator.

    Because of the break statement, I don't see how this could happen with that code.

    Without the break, you are erasing from the vector while iterating over it, which invalidates the iterator you are using. You would have to use the return value of erase instead of incrementing the iterator. The typical solution with a loop looks like this:
    Code:
    vector<Building>::iterator itBuilding = buildings.begin();
    while (itBuilding != buildings.end()) {
    	if (itBuilding->isPointInObject(&downPoint)) {
    		itBuilding = buildings.erase(itBuilding);
    	}
    	else {
    		++itBuilding;
    	}
    }
    But again, the break should avoid those issues, so I don't know what is wrong here.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    that was my thinking, that the break would avoid this problem, and to be more clear, this is a runtime problem, it compiles fine.

    I haven't tested your suggestion yet, I'm not sure if it will yield any different results, I'd imagine the end result assembly will look fairly similar, but I'll try it out tomorrow unless if anyone has some other suggestions.

    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you break into the debugger, you can go back up the call stack to this code and see what the variables are. You can dig around the iterator code to see if it you can tell if it is invalid. You can check the size of the vector.

    It is possible that the vector itself is invalid. Is it a member variable of a deleted object? Check to see if it is possible that the vector itself is corrupted at that point in the code.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    ok, so it turns out it had nothing to do with the vector code above. Using the debugger properly as you suggested I was able to see exactly where it was screwing up. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM