Results 1 to 3 of 3

Thread: help with templates please

  1. #1
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default help with templates please

    Hi
    I have the following code defining a template class
    Qt Code:
    1. template<class T>
    2. class TemplateClass
    3. {
    4. public:
    5. TemplateClass();
    6. virtual ~TemplateClass() {}
    7. void add (T key, QString value);
    8. void get(QStringList &theStrings) const;
    9.  
    10. private:
    11. QMap<T,QString> m_strings;
    12.  
    13. };
    14.  
    15. template<class T> TemplateClass<T>::TemplateClass()
    16. {
    17.  
    18. }
    19.  
    20. template<class T> void TemplateClass<T>::add (T key, QString value)
    21. {
    22. m_strings[key] = value;
    23. }
    24.  
    25. template<class T> void TemplateClass<T>::getComboBoxStrings(QStringList &theStrings) const
    26. {
    27. QMap<T,QString>::iterator it;
    To copy to clipboard, switch view to plain text mode 

    When compiled I get the following error regarding the declaration of the iterator

    Qt Code:
    1. MyClass.h: In member function 'void TemplateClass<T>::getComboBoxStrings(QStringList&) const':
    2. MyClass.h:74: error: expected ';' before 'it'
    To copy to clipboard, switch view to plain text mode 

    Could someone tell me why this is and error

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: help with templates please

    Qt Code:
    1. QMap<T,QString>::iterator it;
    To copy to clipboard, switch view to plain text mode 
    Because compiler does not know that "iterator" is a typename. You need to specify that:
    Qt Code:
    1. typename QMap<T,QString>::iterator it;
    To copy to clipboard, switch view to plain text mode 

    ----------------
    I see that g++ (v 4.5.2) produces a nice message:
    Qt Code:
    1. template <class C> class Test{
    2. public:
    3. void fun();
    4. };
    5.  
    6. template<class C> void Test<C>::fun(){
    7. C::iterator it;
    8. }
    9.  
    10. // error: need 'typename' before 'C:: iterator' because 'C' is a dependent scope
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 4th February 2011 at 17:27. Reason: additional comment

  3. The following user says thank you to stampede for this useful post:

    GrahamLabdon (7th February 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help with templates please

    Use the typename keyword.

    Qt Code:
    1. template<class T> void TemplateClass<T>::getComboBoxStrings(QStringList &theStrings) const
    2. {
    3. typename QMap<T,QString>::const_iterator it;
    To copy to clipboard, switch view to plain text mode 

    When you use a "compile time unresolved class" (class defined by a template argument) you must suggest the compiler that this class has a type named "const_iterator"
    A camel can go 14 days without drink,
    I can't!!!

  5. The following user says thank you to mcosta for this useful post:

    GrahamLabdon (7th February 2011)

Similar Threads

  1. Templates Confusion
    By baray98 in forum General Programming
    Replies: 6
    Last Post: 23rd November 2008, 11:14
  2. operator<< and templates
    By mickey in forum General Programming
    Replies: 3
    Last Post: 30th May 2008, 17:13
  3. templates
    By mickey in forum General Programming
    Replies: 5
    Last Post: 16th January 2008, 15:25
  4. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01
  5. Templates : Help Please
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 14th February 2006, 13:50

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
  •  
Qt is a trademark of The Qt Company.