Thread: Vector of objects and Iterator

  1. #1
    Registered User
    Join Date
    Sep 2012

    Vector of objects and Iterator

    hi, I am getting runtimes errors sometimes and cant figure out why, that i am pointing on wrong memory.
    i have got this terrible interface. I think there can be problem with saving objects to vector and my copy constructor, or iterator. And i am not sure if i am saving those files correctly.

    Thanks for any help.

    I have class Airport which has vector of planes.

    vector<plane> planes;
    vector<plane>::iterator planeIterator;
    bool airport::addPlane(const plane& p)   //funcction to add planes into vector
            planeIterator = planes.begin();     //if vector empty put iterator on first object
        return true;
    const plane* airport::getCurrentPlane() const    // get the current object on which is iterator pointing has to be const pointer on plane
        if(planes.size()== 0)
            return NULL;
        plane *p1 = new plane(*planeIterator);
        return p1;
    //here is my copy constructor from plane class
    plane::plane(const plane& other)
        _hasLanded = other._hasLanded;

  2. #2
    Registered User
    Join Date
    Jun 2005
    Any operation that changes number of elements in a vector - such as push_back() - can invalidate all iterators related to that vector.

    Every call to plane.push_back() therefore needs to be followed up by "planeIterator = plan.begin()". Similarly with any operation that removes elements from the plane vector.
    Last edited by grumpy; 01-06-2013 at 07:02 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2010
    I worry that you declared these variables as global:
    vector<plane> planes;
    vector<plane>::iterator planeIterator;
    I hope you only wanted to make this code snippet as small as possible. If you did not, you should make them members of `airport`.
    Remember to also properly implement copy constructor and copy-assignment operator of `airport`, or prohibit its copying at all.

    And seriously consider using an index instead of an iterator - you will avoid many errors and you will be able to use the default copy constructor/copy-assignment operator.

  4. #4
    Join Date
    Oct 2007
    I would also advice you to get rid of the new. Just return a const reference to the object pointed to by the iterator (or in this case, always .begin())!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with vector iterator
    By Programmer_P in forum C++ Programming
    Replies: 39
    Last Post: 01-10-2013, 08:19 AM
  2. vector and iterator problem
    By BeBu in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2009, 07:38 AM
  3. Vector Iterator Help
    By (TNT) in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2007, 01:53 PM
  4. stl vector + iterator operation
    By sujeet1 in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2007, 04:10 PM
  5. vector/iterator program
    By Drake in forum Game Programming
    Replies: 3
    Last Post: 02-06-2006, 12:55 PM