Jump to content
  • Advertisement
Sign in to follow this  
owl

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

This topic is 2753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!