Results 1 to 8 of 8

Thread: how to write a template function in a form..

  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default how to write a template function in a form..

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to write a template function in a form..

    Is this a problem with Designer or with C++?

  3. #3
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Re: how to write a template function in a form..

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to write a template function in a form..

    Does the same template function implemented outside Designer work?

  5. #5
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to write a template function in a form..

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to write a template function in a form..

    Quote Originally Posted by nass View Post
    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...

  7. #7
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to write a template function in a form..

    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 :

    Qt Code:
    1. template <class T>
    2. void myForm<T>::setVars( T num,const float &lowLim, const float&highLim, const float &theSmallStep,const float &theBigStep)
    3. {
    4. ...
    5. shownum<T>(num)
    6. ...
    7. }
    8.  
    9. template <class T>
    10. void myForm<T>::showNum( const T& num )
    11. {
    12. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. #include <qapplication.h>
    2. #include "myForm.h"
    3.  
    4. int main( int argc, char ** argv )
    5. {
    6. QApplication a( argc, argv );
    7. myForm w;
    8.  
    9. //i only added the following line
    10. w.setVars<int>(9,0,60,1,10);
    11.  
    12. w.show();
    13. a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by nass; 18th December 2006 at 18:38.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to write a template function in a form..

    Template methods have to be defined in the class header. Maybe that is your problem...

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52
  5. Replies: 25
    Last Post: 15th January 2006, 00:53

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.