Results 1 to 6 of 6

Thread: Std iterator as private member variable

  1. #1
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Std iterator as private member variable

    I have the following header file:

    Qt Code:
    1. #include <list>
    2. using namespace std;
    3.  
    4. template <class T>
    5. class QuickList {
    6. public:
    7. // Some public things
    8.  
    9. private:
    10. list<T> _theList;
    11. mutable list<T>::iterator _currentPointer;
    12. };
    To copy to clipboard, switch view to plain text mode 

    When I try to compile, I get the following errors (line numbers changed to correspond with the code above:

    In file included from quicklist.cpp:1:
    quicklist.h:11: error: type `std::list<T, std::allocator<_CharT> >' is not derived from type `QuickList<T>'
    quicklist.h:11: error: ISO C++ forbids declaration of `iterator' with no type
    quicklist.h:11: error: expected `;' before "_currentPointer"
    I need the mutable keyword if I want to keep my consts intact. However, if I remove it, I still get this single error:

    In file included from quicklist.cpp:1:
    quicklist.h:11: error: expected `;' before "_currentPointer"
    I don't understand these errors. What am I doing wrong?

    Could someone help me with this?

    Thanks!
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  2. #2
    Join Date
    Jan 2006
    Posts
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Std iterator as private member variable

    What is in the quicklist.cpp file? One common problem with templates is that definition AND declaration need to be in de .h file. I think you must move your implementation from the .cpp file to the .h file

  3. #3
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Std iterator as private member variable

    I am not sure about mutable but you need to use typename:
    mutable typename list<T>::iterator _currentPointer;
    will compile
    Last edited by jcr; 21st April 2006 at 16:08.

  4. The following user says thank you to jcr for this useful post:

    Michiel (21st April 2006)

  5. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Std iterator as private member variable

    typename, huh? I've never seen that one before. But it works! With mutable and everything. Thanks!

    When exactly does one use typename?

    And edb, I am aware of that problem. But apparently g++ has solved it. Because it now compiles perfectly. (And if, later, it seems not to work, I'll just move everything to the headerfile again.)
    Last edited by Michiel; 21st April 2006 at 16:13.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #5
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Std iterator as private member variable

    The compiler needs to know without any ambiguity that list<T>::iterator is the name of a type and not something else. Inside std::list, iterator could be something else than a type. You can find litterature on that topic in most C++ books; Stroustrup in C++ Programing Language Section C.13.5 for instance.

  7. #6
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Std iterator as private member variable

    Ah, because T has not yet been determined. I suppose someone could create a totally seperate std::list<int> class in which iterator is not a type.

    Thanks.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. Replies: 22
    Last Post: 8th October 2008, 14:54
  2. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 11:15
  3. QTime private member mds means what?
    By jamadagni in forum Qt Programming
    Replies: 2
    Last Post: 24th March 2006, 19:08

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.