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. Arrays of functions help
Forum Updated to NodeBB v4.3 + New Features

Arrays of functions help

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 3.1k Views 3 Watching
  • 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #4

    I tried it and it works. did you forget to #include <functional>?

    "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

    B 1 Reply Last reply
    0
    • VRoninV VRonin

      I tried it and it works. did you forget to #include <functional>?

      B Offline
      B Offline
      bwcal1999
      wrote on last edited by
      #5

      @VRonin Yes i included that.

      I get the error
      'function' is not a member of 'std' std::function<void()> functions[10];
      ^

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #6

        try this code:

        #include <functional>
        #include <QDebug>
        class MyClass{
        public:
            MyClass()=default;
            void on_pushButton_clicked()
            {
            std::array<std::function<void()>,10> functions;
            for(int i = 0; i < 10; i++)
                    functions[i] = std::bind( &MyClass::readMe,this);
            for(int i = 0; i < 10; i++)
                functions[i]();
            }
            void readMe()
            {
                qDebug() << "I got read";
            }
        
        };
        
        int main()
        {
        MyClass tempClass;
        tempClass.on_pushButton_clicked();
        return 0;
        }
        

        It works for me... Make sure your pro file contains:

        CONFIG += c++11

        and re-run qmake

        EDIT:
        @Wieland is correct, std::array is preferable
        There is so much C++11 here I'm having an overdose!

        "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

        B 1 Reply Last reply
        1
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #7

          Hi! Or use a lambda:

          void MainWindow::sayHi()
          {
              qDebug() << "Hi!";
          }
          
          void MainWindow::on_pushButton_clicked()
          {
              auto const count{10};
              std::array<std::function<void()>,count> myArray;
              for (auto &i : myArray) {
                  i = [=]() { this->sayHi(); };
              }
          }
          
          1 Reply Last reply
          2
          • VRoninV VRonin

            try this code:

            #include <functional>
            #include <QDebug>
            class MyClass{
            public:
                MyClass()=default;
                void on_pushButton_clicked()
                {
                std::array<std::function<void()>,10> functions;
                for(int i = 0; i < 10; i++)
                        functions[i] = std::bind( &MyClass::readMe,this);
                for(int i = 0; i < 10; i++)
                    functions[i]();
                }
                void readMe()
                {
                    qDebug() << "I got read";
                }
            
            };
            
            int main()
            {
            MyClass tempClass;
            tempClass.on_pushButton_clicked();
            return 0;
            }
            

            It works for me... Make sure your pro file contains:

            CONFIG += c++11

            and re-run qmake

            EDIT:
            @Wieland is correct, std::array is preferable
            There is so much C++11 here I'm having an overdose!

            B Offline
            B Offline
            bwcal1999
            wrote on last edited by
            #8

            @VRonin said in Arrays of functions help:

            CONFIG += c++11

            I am using QTCreator 5.5.1 and still get the same error. not a member of std with both function and bind.

            Also with g++ it i get the same error.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #9

              In Qt Creator click on build->run qmake after you put CONFIG += c++11 in your .pro 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

              B 1 Reply Last reply
              0
              • VRoninV VRonin

                In Qt Creator click on build->run qmake after you put CONFIG += c++11 in your .pro file

                B Offline
                B Offline
                bwcal1999
                wrote on last edited by
                #10

                @VRonin yes I have done that several times.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #11

                  let's go C++11 mental!

                  try this code in your main.cpp commenting out all the rest that's already there and tell me if it works and if not what does it complain about

                  #include <functional>
                  #include <array>
                  #include <QDebug>
                  class MyClass{
                  public:
                      void on_pushButton_clicked()
                      {
                      std::array<std::function<void()>,10> functions;
                      std::fill(std::begin(functions),std::end(functions),std::bind( &MyClass::readMe,this));
                      for(const auto& i : functions)
                          i();
                      }
                      void readMe()
                      {
                          qDebug() << "I got read";
                      }
                  
                  };
                  int main()
                  {
                  MyClass tempClass;
                  tempClass.on_pushButton_clicked();
                  return 0;
                  }
                  

                  "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

                  ? 1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #12

                    This is the C++98 version of the code above

                    #include <QDebug>
                    
                    class MyClass{
                    public:
                    
                        void on_pushButton_clicked()
                        {
                    
                        void (MyClass::*functions[10])() ;
                        for(int i = 0; i < 10; ++i)
                            functions[i] = &MyClass::readMe;
                        for(int i = 0; i < 10; ++i)
                            (this->*functions[i])();
                        }
                        void readMe()
                        {
                    
                            qDebug() <<  "I got read";
                        }
                    
                    };
                    
                    int main(int argc, char **argv)
                    {
                    MyClass tempClass;
                    tempClass.on_pushButton_clicked();
                    return 0;
                    }
                    

                    "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

                    ? kshegunovK 2 Replies Last reply
                    0
                    • VRoninV VRonin

                      This is the C++98 version of the code above

                      #include <QDebug>
                      
                      class MyClass{
                      public:
                      
                          void on_pushButton_clicked()
                          {
                      
                          void (MyClass::*functions[10])() ;
                          for(int i = 0; i < 10; ++i)
                              functions[i] = &MyClass::readMe;
                          for(int i = 0; i < 10; ++i)
                              (this->*functions[i])();
                          }
                          void readMe()
                          {
                      
                              qDebug() <<  "I got read";
                          }
                      
                      };
                      
                      int main(int argc, char **argv)
                      {
                      MyClass tempClass;
                      tempClass.on_pushButton_clicked();
                      return 0;
                      }
                      
                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #13

                      @VRonin This is so retro, i want to watch Miami Vice on VHS now :-D

                      1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @Wieland Starwars on Super 8mm ;)

                        @bwcal1999 What Qt version, compiler (including version), OS are you using ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • VRoninV VRonin

                          This is the C++98 version of the code above

                          #include <QDebug>
                          
                          class MyClass{
                          public:
                          
                              void on_pushButton_clicked()
                              {
                          
                              void (MyClass::*functions[10])() ;
                              for(int i = 0; i < 10; ++i)
                                  functions[i] = &MyClass::readMe;
                              for(int i = 0; i < 10; ++i)
                                  (this->*functions[i])();
                              }
                              void readMe()
                              {
                          
                                  qDebug() <<  "I got read";
                              }
                          
                          };
                          
                          int main(int argc, char **argv)
                          {
                          MyClass tempClass;
                          tempClass.on_pushButton_clicked();
                          return 0;
                          }
                          
                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #15

                          @VRonin said in Arrays of functions help:

                          void (MyClass::*functions[10])() ;
                          

                          I hope you acknowledge most of us dinosaurs will typedef that. We are not masochists, only old-fashioned. ;)

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • VRoninV VRonin

                            let's go C++11 mental!

                            try this code in your main.cpp commenting out all the rest that's already there and tell me if it works and if not what does it complain about

                            #include <functional>
                            #include <array>
                            #include <QDebug>
                            class MyClass{
                            public:
                                void on_pushButton_clicked()
                                {
                                std::array<std::function<void()>,10> functions;
                                std::fill(std::begin(functions),std::end(functions),std::bind( &MyClass::readMe,this));
                                for(const auto& i : functions)
                                    i();
                                }
                                void readMe()
                                {
                                    qDebug() << "I got read";
                                }
                            
                            };
                            int main()
                            {
                            MyClass tempClass;
                            tempClass.on_pushButton_clicked();
                            return 0;
                            }
                            
                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by A Former User
                            #16

                            @VRonin said in Arrays of functions help:

                            let's go C++11 mental!

                            Okay :-)

                            void MainWindow::on_pushButton_clicked()
                            {
                                std::array<std::function<void()>, 10> f;
                                std::fill(std::begin(f), std::end(f), [=](){this->sayHi();});
                                std::for_each(std::begin(f), std::end(f), [](auto &i){i();});
                            }
                            
                            1 Reply Last reply
                            3

                            • Login

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