PDA

View Full Version : template compilation problem



totem
19th October 2010, 08:39
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 :


#ifndef _MY_CLASS1_H_
#define _MY_CLASS1_H_

#include <QObject>

class MyClass1 : public QObject
{
Q_OBJECT

public :

MyClass1(QObject *parent=0) ;
virtual ~MyClass1(void) ;
} ;

#endif // _MY_CLASS1_H_


class1.cpp code :


#include "header1.h"
#include "header2.h"

MyClass1::MyClass1(QObject *parent)
: QObject(parent)
{
}


MyClass1::~MyClass1()
{
}



header2.h code :



#ifndef _MY_CLASS2_H_
#define _MY_CLASS2_H_

#include <QObject>
#include <QList>

class MyClass2 : public QObject
{
Q_OBJECT

public :

MyClass2(QObject *parent=0) ;
virtual ~MyClass2(void) ;

template<typename T>
QList<T*> listOfTypedObjects(void) const ;
} ;

template<typename T>
QList<T*>
MyClass2::listOfTypedObjects(void) const
{
QList<T*> result1 ;
QList<T*> result2 ;

QList<T*>::const_iterator it ;
for(it=result1.begin(); it!=result1.end(); it++)
result2.push_back(*it) ;

return result2 ;
}
#endif // _MY_CLASS2_H_

class2.cpp code :


#include "header2.h"

MyClass2::MyClass2(QObject *parent)
: QObject(parent)
{
}


MyClass2::~MyClass2()
{
}



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.
5360

Any idea what I'm missing ?

high_flyer
19th October 2010, 13:51
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.

high_flyer
19th October 2010, 13:56
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.

totem
19th October 2010, 14:34
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


typename QList<T*>::const_iterator it ;

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