Results 1 to 4 of 4

Thread: template compilation problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default template compilation problem

    Hi,

    I'm facing a compilation problem : I don't have it with visual studio compiler, but when I try to compile the same code using QtCreator with Linux (and apparently reproduced on Mac), an error appears.

    header1.h code :
    Qt Code:
    1. #ifndef _MY_CLASS1_H_
    2. #define _MY_CLASS1_H_
    3.  
    4. #include <QObject>
    5.  
    6. class MyClass1 : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public :
    11.  
    12. MyClass1(QObject *parent=0) ;
    13. virtual ~MyClass1(void) ;
    14. } ;
    15.  
    16. #endif // _MY_CLASS1_H_
    To copy to clipboard, switch view to plain text mode 

    class1.cpp code :
    Qt Code:
    1. #include "header1.h"
    2. #include "header2.h"
    3.  
    4. MyClass1::MyClass1(QObject *parent)
    5. : QObject(parent)
    6. {
    7. }
    8.  
    9.  
    10. MyClass1::~MyClass1()
    11. {
    12. }
    To copy to clipboard, switch view to plain text mode 


    header2.h code :

    Qt Code:
    1. #ifndef _MY_CLASS2_H_
    2. #define _MY_CLASS2_H_
    3.  
    4. #include <QObject>
    5. #include <QList>
    6.  
    7. class MyClass2 : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public :
    12.  
    13. MyClass2(QObject *parent=0) ;
    14. virtual ~MyClass2(void) ;
    15.  
    16. template<typename T>
    17. QList<T*> listOfTypedObjects(void) const ;
    18. } ;
    19.  
    20. template<typename T>
    21. QList<T*>
    22. MyClass2::listOfTypedObjects(void) const
    23. {
    24. QList<T*> result1 ;
    25. QList<T*> result2 ;
    26.  
    27. QList<T*>::const_iterator it ;
    28. for(it=result1.begin(); it!=result1.end(); it++)
    29. result2.push_back(*it) ;
    30.  
    31. return result2 ;
    32. }
    33. #endif // _MY_CLASS2_H_
    To copy to clipboard, switch view to plain text mode 

    class2.cpp code :
    Qt Code:
    1. #include "header2.h"
    2.  
    3. MyClass2::MyClass2(QObject *parent)
    4. : QObject(parent)
    5. {
    6. }
    7.  
    8.  
    9. MyClass2::~MyClass2()
    10. {
    11. }
    To copy to clipboard, switch view to plain text mode 


    The error points to MyClass2::listOfTypedObjects method body - and more specifically to the iterator declaration - and tells something like :

    line 27 : expected ';' before it
    as if It did not recognize QList<T*>::const_iterator definition.

    I attached a small example reproducing the problem.
    templates.zip

    Any idea what I'm missing ?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: template compilation problem

    I can't see in your code, when T is getting resolved. (that is, when it gets to be a specific type).
    This might also be the problem GCC has.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: template compilation problem

    Quote Originally Posted by high_flyer View Post
    I can't see in your code, when T is getting resolved. (that is, when it gets to be a specific type).
    This might also be the problem GCC has.
    in fact the template method is not called, that's why I was confused.
    Anyway, I think I found a solution, though I did not test it yet : add the keyword typename in front of the iterator declaration

    Qt Code:
    1. typename QList<T*>::const_iterator it ;
    To copy to clipboard, switch view to plain text mode 

    After all it seems logical, but VS compiler does not complain about this lack.

    Edit: adding 'typename' actually fixed the compilation issue. Thanks anyway high_flyer
    Last edited by totem; 19th October 2010 at 20:29. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: template compilation problem

    Oh, and in addition, as far as I know, QObjects and templates don't mix well - more like don't mix at all due to the meta object system, that can't handle it.
    AFAIK.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. problem with using template classes
    By arbi in forum General Programming
    Replies: 4
    Last Post: 1st September 2010, 13:29
  2. Replies: 4
    Last Post: 25th June 2010, 09:21
  3. qt4 compilation problem
    By aegis in forum Installation and Deployment
    Replies: 9
    Last Post: 22nd February 2007, 23:10
  4. problem using template
    By mickey in forum General Programming
    Replies: 6
    Last Post: 18th November 2006, 15:57
  5. Compilation problem, don't know why
    By yellowmat in forum Newbie
    Replies: 6
    Last Post: 2nd March 2006, 15:36

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.