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

Started by
6 comments, last by Decrius 13 years, 4 months ago
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.
[size="2"]I like the Walrus best.
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.
[size="2"]I like the Walrus best.
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?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
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;        }    }


[size="2"]I like the Walrus best.
I'd advice to use the reverse iterators:
http://www.cplusplus.com/reference/stl/vector/rend/
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
++ for the tip
[size="2"]I like the Walrus best.
Quote:Original post by owl

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


Yes. Buy a rubber duck.
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!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

This topic is closed to new replies.

Advertisement