hi, I'm trying to use an my own iterator on my own stack;
But in main.cpp while loop doesn't work. Could anyone read my code?
thanks
hi, I'm trying to use an my own iterator on my own stack;
But in main.cpp while loop doesn't work. Could anyone read my code?
thanks
Regards
You have to fix operator !=, begin(), end() and remove().
Some hints:
1. Two iterators are equal when they point to the same node.
2. head points to the beginning of the stack.
I don't understand fine how the iterators costructor work....
anyway, I don't understand what want you say with your 2nd hint: beginning = top stack? If I understand fine, in this code, head points to the top element (the last inserted). Maybe Are you saying that must be the opposite?
Regards
hi,
I changed a little bit and seems ok. But I'm wondering:
why can I use :
and why that error inside op<< (see stack.h)?Qt Code:
cout << it.getValue() << endl; and not cout << (*it).getValue() << endl;To copy to clipboard, switch view to plain text mode
thanks
Regards
and in remove what doesn't work...? the way of delete?
and what way to avoid this?
Qt Code:
warning C4800: 'Stack<Type,size>::Node *' : forcing value to bool 'true' or 'false' (performance warning) with [ Type=int, size=20 ]To copy to clipboard, switch view to plain text mode
Regards
In the first version you had:
iterator::iterator() creates an iterator that points nowhere, because current == 0 and iterator::iterator( Stack<Type,size>& ) creates an iterator that points to the top of the stack.Qt Code:
iterator end() const { cout << "end " << iterator(*this) << endl; return iterator( *this); } iterator begin() const { cout << "begin() " << iterator() << endl; return iterator( ); }To copy to clipboard, switch view to plain text mode
Now, you would like to use those iterators like this:
But the problem is that begin() returns an invalid iterator (because it uses iterator::iterator()), so ++it doesn't work.Qt Code:
for( Stack<...>::iterator it = stack.begin(); it != stack.end(); ++it ) { // ... }To copy to clipboard, switch view to plain text mode
Because "it" is an object not a pointer.
how can I declare iterator to use (*it).other_method and 'it = l->begin()'??
Regards
Bookmarks