PDA

View Full Version : how to write a template function in a form..



nass
18th December 2006, 17:18
hello everyone,
if i want to add a template function in a form from within qt designer, what steps should i follow?
please be specific cause apart from my influent knowledge of qtdesigner, i have practically zero knowledge on templates..
so far i have typed 'template <class T>' on the line right above my fn implementations

template <class T>
void setValueForm<T>::setVars( T num,const float &lowLim, const float&highLim, const float &theSmallStep,const float &theBigStep)
{
...
shownum<T>(num)
...
}

template <class T>
void setValueForm<T>::showNum( const T& num )
{
}

and i also tried to add in the forward declarations field 'template <class T>'. but it does not compile

thank you for your help
nass

wysota
18th December 2006, 18:08
Is this a problem with Designer or with C++?

nass
18th December 2006, 18:10
i guess with the designer... cause i did read a c++ stl tutorial befor setting out to convert my function to a template function...
so i added al lthe necessary template calls ... i think.

wysota
18th December 2006, 18:34
Does the same template function implemented outside Designer work?

nass
18th December 2006, 18:40
i have not tested that, i have however altered directly the .ui/myForm.h:
i remove the ";" from the declaration
template <class T>;
class MyForm
{
...
}

and it compiled.
my question is what steps should i follow in order to declare a function (not the whole class but just a function of the class) as template so that it can process both floats and ints.
nass

wysota
18th December 2006, 19:14
my question is what steps should i follow in order to declare a function (not the whole class but just a function of the class) as template so that it can process both floats and ints.

Declare the function as template<class T> and implement it substituting template types (int, float) with "T".

This really isn't a Designer question...

nass
18th December 2006, 19:29
that does not seem to be enough, let me explain in detail:

there is the file myForm.ui.h, ie an extension of the .ui/myForm.cpp:

in that file there a couple of fns that i want to use as templates. these i mention above and declared as such :


template <class T>
void myForm<T>::setVars( T num,const float &lowLim, const float&highLim, const float &theSmallStep,const float &theBigStep)
{
...
shownum<T>(num)
...
}

template <class T>
void myForm<T>::showNum( const T& num )
{
}


then there is the file .ui/myForm.h

i guess i should not add or change anything here.

finally there is a trivial main.cpp generated by QtDesigner:


#include <qapplication.h>
#include "myForm.h"

int main( int argc, char ** argv )
{
QApplication a( argc, argv );
myForm w;

//i only added the following line
w.setVars<int>(9,0,60,1,10);

w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}


these are the steps i just did... yet when i compile:


In file included from main.cpp:2:
.ui/setValueForm.h:48: error: syntax error before `&' token
.ui/setValueForm.h:49: error: `T' was not declared in this scope
.ui/setValueForm.h:49: error: syntax error before `,' token
main.cpp: In function `int main(int, char**)':
main.cpp:8: error: syntax error before `>' token
make: *** [.obj/main.o] Error 1

these lines are public methods of class myForm:
.ui/setValueForm.h:48: virtual void showNum( const T & num );
.ui/setValueForm.h:49: virtual void setVars( T num, const float & lowLim, const float & highLim, const float & theSmallStep, const float & theBigStep );
main.cpp:8: w.setVars<int> (9,0,60,1,10);


note that before when i said i had managed to make it compile i meant i had managed to make the class template, not solely the 2 functions.

i hope this will clarify things abit. Thank you for your help anyway.
nass

wysota
18th December 2006, 20:36
Template methods have to be defined in the class header. Maybe that is your problem...