How to write template functions?



  • Hi i am new to Qt and learning to implement template/generic functions in Qt.
    After reading a small tutorial i started to try a small example

    HeaderFile

    template <class ftmp,class operation_str> int sample(ftmp,operation_str);
    class Example : public QWidget
    {
         Q_OBJECT
         public:
         Example();
         sample(ftmp,operation_str);
    };
    

    CPP

    Example::Example()
    {
         sample(1,"Hi");
    }
    int Example::sample(ftmp fno,operation_str ops)
    {
        qDebug()<<"Data if FNo and ops:"<<fno<<ops;
    }
    

    While compiling i am getting error

    error: 'ftmp' has not been declared
    error: 'operation_str' has not been declared
    

    Is this way is possible to do..?
    I want to make the function member of a class inorder to use it and i want to call the sample from other CPP files in coming future, am i doing correct..?

    Please guide me

    Thanks in advance,
    Mounika.P


  • Moderators

    @mounipanditi This is completely unrelated to Qt: Qt is not a programming language, C++ is.
    You should read that tutorial more careful or, even better, a C++ book.
    Templates must be in a header file, not cpp file.
    This is wrong:

    template <class ftmp,class operation_str> int sample(ftmp,operation_str);
    class Example : public QWidget
    {
         Q_OBJECT
         public:
         Example();
         sample(ftmp,operation_str);
    };
    

    If you want sample to be a member function of your template then put it there:

    template <class ftmp,class operation_str>
    class Example : public QWidget
    {
         Q_OBJECT
         public:
         Example() { sample(1,"Hi"); }
         int sample(ftmp,operation_str) { /* You need to put the implementation here as it is not possible to put template implementation into a cpp file*/}
    };
    
    


  • To add to @jsulm you cannot put Q_OBJECT inside template classes, moc is not that smart. the alternatives are:

    class Example : public QWidget
    {
         Q_OBJECT
         public:
         Example() { sample(1,"Hi"); }
    template <class ftmp,class operation_str>
         int sample(ftmp,operation_str) { /* You need to put the implementation here as it is not possible to put template implementation into a cpp file*/}
    };
    

    or

    class BaseExample : public QWidget{
    Q_OBJECT
    public:
    // stuff
    signals:
    // stuff
    public slots:
    // stuff
    };
    template <class ftmp,class operation_str>
    class Example : public BaseExample 
    {
         public:
         Example() { sample(1,"Hi"); }
         int sample(ftmp,operation_str) { /* You need to put the implementation here as it is not possible to put template implementation into a cpp file*/}
    };
    


  • @VRonin

    Thanks for replying

    I have understood the explanation given by you

    I am having something more to ask, i just want to know if the following requirement is possible or not

    assume that function sample have to accept parameters from another CPP file or if i want to call the sample function from some other CPP file and it should pass the parameters to some other function which is present in another CPP file.

    Is it possible if so how can i achieve it please post a code snippet.

    Test.cpp

    #include "baseExample.h"
    
    Test::Test()
    {
       Example e;
       e.sample(2,"pass");
    }
    
    //Inside the class Example 
    int sample(ftmp f,operation_str s)
    {
         some_other_function(f,s);
    }
    

    Sorry if my questions bothers you,i just want to learn.

    Thanks in advance,
    Mounika.P



  • @mounipanditi said in How to write template functions in Qt:

    assume that function sample have to accept parameters from another CPP file or if i want to call the sample function from some other CPP file and it should pass the parameters to some other function which is present in another CPP file.

    Did not get what you mean here but the code snippet works fine



  • @VRonin

    The sample function which is present in Example class need to accept Parameters from some other class named Test and it should forward the parameters that it received to some other function present in a class named Experiment.

    Is this possible..!

    Thanks in advance,
    Mounika.P



  • Yes it is



  • @VRonin

    please post a sample code snippet



  • class Example : public QWidget
    {
         Q_OBJECT
         public:
         Example() { sample(1,"Hi"); }
    template <class ftmp,class operation_str>
         int sample(ftmp f ,operation_str s) { Experiment temp; temp.something_else(f,s);}
    };
    class Test{
    public:
    void something(){
    Example temp;
    temp.sample(2.5,"Hello");
    }
    };
    class Experiment{
    public:
    template <class ftmp,class operation_str>
    void something_else(ftmp f, operation_str s){
    qDebug() << f << s;
    }
    };
    int main(int argc, char*[] argv){
    QApplication app(argc,argv);
    Test var1;
    var1.something();
    return app.exec();
    }
    
    

    see http://www.cplusplus.com/doc/tutorial/templates/#class_templates



  • @VRonin

    Thanks for the snippet and special thanks for sharing the link.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.