Vector of objects and Iterator

This is a discussion on Vector of objects and Iterator within the C++ Programming forums, part of the General Programming Boards category; hi, I am getting runtimes errors sometimes and cant figure out why, that i am pointing on wrong memory. i ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    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.

    Code:
    vector<plane> planes;
    vector<plane>::iterator planeIterator;
    
    
    bool airport::addPlane(const plane& p)   //funcction to add planes into vector
    {
        if(planes.empty())
        {
            planes.push_back(p);
            planeIterator = planes.begin();     //if vector empty put iterator on first object
        }
        else
        {
            planes.push_back(p);
        }
        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)
    {
        _name=other.getName();
        other.getPosition(_x,_y);
        _direction=other.getDirection();
        _hasLanded = other._hasLanded;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    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 08: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, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    I worry that you declared these variables as global:
    Code:
    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.
    I never put signature, but I decided to make an exception.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    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, 09:19 AM
  2. vector and iterator problem
    By BeBu in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2009, 08:38 AM
  3. Vector Iterator Help
    By (TNT) in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2007, 02:53 PM
  4. stl vector + iterator operation
    By sujeet1 in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2007, 05:10 PM
  5. vector/iterator program
    By Drake in forum Game Programming
    Replies: 3
    Last Post: 02-06-2006, 01:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21