Sign in to follow this  
owl

Problem with vector::iterator (actually with a class I use to encapsulate it)

Recommended Posts

In this class I sort of encapsulate the functionality of vector.


template <class TVal>
class vector
{
private:
std::vector<TVal> m_container;
typename std::vector<TVal>::iterator m_item;

public:
vector ( void ) {m_item = m_container.begin();}
virtual ~vector ( void ) {}

inline void move_first() { m_item = m_container.begin(); }
inline void move_last() { m_item = --m_container.end(); }
inline bool move_next() { ++m_item; return !eof();}
inline bool operator ++() { return move_next(); }
inline bool move_previous() { --m_item; return !bof(); }
inline bool bof() { return (m_container.begin()==m_item); }
inline bool eof() { return (m_container.end()==m_item); }
inline void value(const TVal& val) { *m_item = val; }
inline TVal& value() { return *m_item; }
inline void insert_front(const TVal& val) { m_container.push_front(val);}
inline void insert_back(const TVal& val) { m_container.push_back(val);}
inline bool erase_current() { return m_container.erase(m_item); }
inline void erase_last() { m_container.pop_back(); }
inline void clear() { m_container.clear(); }
inline size_t size() { return m_container.size(); }
inline void reserve(size_t size) { m_container.reserve(size); }
inline void resize(size_t size) { m_container.resize(size); }


};





I have an instance of this class with 3 elements (boost::shared_ptr's) in it.
When I try to iterate it with a while loop, at the second iteration (that'd be the second element starting from the end) I'm getting a null pointer exception.

my_vector.move_last();

while (!my_vector.bof())
{
std::cout << my_vector.value() <<std::endl;
if (my_vector.value()->handle_mouse(e))
{
handled = true;
break;
}
my_vector.move_previous();
}





If I iterate it fowards, it works ok.

my_vector.move_first();
while (!my_vector.eof())
{
if (my_vector.value()->handle_mouse(e))
{
handled = true;
break;
}
my_vector.move_next();
}





Could you throw me a hand at realizing what I am doing wrong?

Thanks.

Share this post


Link to post
Share on other sites
Nevermind. I got it.

It's amazing how posting your own code makes you think about the problem. I've been the last half hour trying to solve this and 2 minutes after posting I saw the problem.

Share this post


Link to post
Share on other sites
Are you sure you can do:

--m_container.end();

I believe end() isn't necessarily AFTER the last element, but is more like a general ending?

Funny, I just encapsulated std::vector today too, to make it usable by a smart pointer :)

Quote:
Original post by owl
Nevermind. I got it.

It's amazing how posting your own code makes you think about the problem. I've been the last half hour trying to solve this and 2 minutes after posting I saw the problem.


What was it?

Share this post


Link to post
Share on other sites
Hi! Exactly what you said. I changed it to:


inline void move_last() { m_item = m_container.end(); }
inline bool move_previous() { if (!bof()) {--m_item; return true;} else return false; }




and I iterate it like this:

children.move_last();
while (children.move_previous())
{
if (children.value()->handle_mouse(e))
{
handled = true;
break;
}
}



Share this post


Link to post
Share on other sites
Quote:
Original post by owl

It's amazing how posting your own code makes you think about the problem.


That is why half of the threads I start typing never make it to the forum...while writing I realize my stupidities!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this