Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to write template functions?
QtWS25 Last Chance

How to write template functions?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 7.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mounipanditi
    wrote on 15 Mar 2017, 07:26 last edited by A Former User
    #1

    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

    J 1 Reply Last reply 15 Mar 2017, 07:34
    0
    • M mounipanditi
      15 Mar 2017, 07:26

      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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 Mar 2017, 07:34 last edited by jsulm
      #2

      @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*/}
      };
      
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • V Offline
        V Offline
        VRonin
        wrote on 15 Mar 2017, 08:13 last edited by VRonin
        #3

        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*/}
        };
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        M 1 Reply Last reply 15 Mar 2017, 11:22
        3
        • V VRonin
          15 Mar 2017, 08:13

          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*/}
          };
          
          M Offline
          M Offline
          mounipanditi
          wrote on 15 Mar 2017, 11:22 last edited by mounipanditi
          #4

          @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

          V 1 Reply Last reply 15 Mar 2017, 11:42
          0
          • M mounipanditi
            15 Mar 2017, 11:22

            @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

            V Offline
            V Offline
            VRonin
            wrote on 15 Mar 2017, 11:42 last edited by
            #5

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply 15 Mar 2017, 11:49
            0
            • V VRonin
              15 Mar 2017, 11:42

              @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

              M Offline
              M Offline
              mounipanditi
              wrote on 15 Mar 2017, 11:49 last edited by
              #6

              @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

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 15 Mar 2017, 12:02 last edited by
                #7

                Yes it is

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                M 1 Reply Last reply 15 Mar 2017, 12:29
                0
                • V VRonin
                  15 Mar 2017, 12:02

                  Yes it is

                  M Offline
                  M Offline
                  mounipanditi
                  wrote on 15 Mar 2017, 12:29 last edited by
                  #8

                  @VRonin

                  please post a sample code snippet

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 15 Mar 2017, 13:47 last edited by
                    #9
                    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

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    M 1 Reply Last reply 15 Mar 2017, 15:35
                    1
                    • V VRonin
                      15 Mar 2017, 13:47
                      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

                      M Offline
                      M Offline
                      mounipanditi
                      wrote on 15 Mar 2017, 15:35 last edited by
                      #10

                      @VRonin

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

                      1 Reply Last reply
                      0

                      8/10

                      15 Mar 2017, 12:29

                      • Login

                      • Login or register to search.
                      8 out of 10
                      • First post
                        8/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved