Results 1 to 8 of 8

Thread: stack

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default stack

    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
    Attached Files Attached Files
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stack

    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.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: 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

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: stack

    hi,
    I changed a little bit and seems ok. But I'm wondering:
    why can I use :
    Qt Code:
    1. cout << it.getValue() << endl;
    2. and not
    3. cout << (*it).getValue() << endl;
    To copy to clipboard, switch view to plain text mode 
    and why that error inside op<< (see stack.h)?
    thanks
    Attached Files Attached Files
    Regards

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: stack

    and in remove what doesn't work...? the way of delete?
    and what way to avoid this?
    Qt Code:
    1. warning C4800: 'Stack<Type,size>::Node *' : forcing value to bool 'true' or 'false' (performance warning)
    2. with
    3. [
    4. Type=int,
    5. size=20
    6. ]
    To copy to clipboard, switch view to plain text mode 
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stack

    Quote Originally Posted by mickey View Post
    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?
    In the first version you had:
    Qt Code:
    1. iterator end() const {
    2. cout << "end " << iterator(*this) << endl;
    3. return iterator( *this);
    4. }
    5.  
    6. iterator begin() const {
    7. cout << "begin() " << iterator() << endl;
    8. return iterator( );
    9. }
    To copy to clipboard, switch view to plain text mode 
    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.

    Now, you would like to use those iterators like this:
    Qt Code:
    1. for( Stack<...>::iterator it = stack.begin(); it != stack.end(); ++it ) {
    2. // ...
    3. }
    To copy to clipboard, switch view to plain text mode 
    But the problem is that begin() returns an invalid iterator (because it uses iterator::iterator()), so ++it doesn't work.

    Quote Originally Posted by mickey View Post
    why can I use :
    cout << it.getValue() << endl;
    and not
    cout << (*it).getValue() << endl;
    Because "it" is an object not a pointer.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: stack

    how can I declare iterator to use (*it).other_method and 'it = l->begin()'??
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stack

    Quote Originally Posted by mickey View Post
    how can I declare iterator to use (*it).other_method and 'it = l->begin()'??
    You have to implement operator* and operator-> and make them return references to the value on the stack.

Similar Threads

  1. Previewing a stack widget dialog
    By mikeh in forum Qt Tools
    Replies: 4
    Last Post: 2nd October 2006, 10:19
  2. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 12:07
  3. QThread stack overflow when exiting
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 11th May 2006, 17:56
  4. QStack Help
    By ankurjain in forum Qt Programming
    Replies: 5
    Last Post: 5th May 2006, 11:44
  5. Template stack, isn't there is a FIFO ?
    By yellowmat in forum Newbie
    Replies: 5
    Last Post: 3rd March 2006, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.