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.0k 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.
  • B Offline
    B Offline
    bwcal1999
    wrote on 28 Oct 2016, 18:33 last edited by
    #1

    I am trying to make an array of functions and can't understand why this is not working. I get the following error when I compile.

    error: cannot convert 'MainWindow::readMe' from type 'void (MainWindow::)()' to type 'arrayOfFunctions {aka void*}'
    functions[i] = readMe;
    ^

    Below is the code.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::readMe()
    {
        qDebug() << "I got read";
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        typedef void (*arrayOfFunctions);
        arrayOfFunctions  functions[10];
    
        for(int i = 0; i < 10; i++)
            functions[i] = readMe;
    
    
    }
    
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 28 Oct 2016, 19:07 last edited by VRonin
      #2

      try

      void MainWindow::on_pushButton_clicked()
      {
      std::function<void()> functions[10];
      for(int i = 0; i < 10; i++)
              functions[i] = std::bind( &MainWindow::readMe,this);
      }
      

      "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 28 Oct 2016, 19:11
      0
      • V VRonin
        28 Oct 2016, 19:07

        try

        void MainWindow::on_pushButton_clicked()
        {
        std::function<void()> functions[10];
        for(int i = 0; i < 10; i++)
                functions[i] = std::bind( &MainWindow::readMe,this);
        }
        
        B Offline
        B Offline
        bwcal1999
        wrote on 28 Oct 2016, 19:11 last edited by
        #3

        @VRonin Nope, that just give a lot more errors.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 28 Oct 2016, 19:12 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 28 Oct 2016, 19:18
          0
          • V VRonin
            28 Oct 2016, 19:12

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

            B Offline
            B Offline
            bwcal1999
            wrote on 28 Oct 2016, 19:18 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
            • V Offline
              V Offline
              VRonin
              wrote on 28 Oct 2016, 19:20 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 28 Oct 2016, 19:31
              1
              • ? Offline
                ? Offline
                A Former User
                wrote on 28 Oct 2016, 19:25 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
                • V VRonin
                  28 Oct 2016, 19:20

                  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 28 Oct 2016, 19:31 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
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 28 Oct 2016, 19:33 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 28 Oct 2016, 19:35
                    0
                    • V VRonin
                      28 Oct 2016, 19:33

                      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 28 Oct 2016, 19:35 last edited by
                      #10

                      @VRonin yes I have done that several times.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        VRonin
                        wrote on 28 Oct 2016, 19:40 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 28 Oct 2016, 22:56
                        0
                        • V Offline
                          V Offline
                          VRonin
                          wrote on 28 Oct 2016, 20:04 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 28 Oct 2016, 20:05
                          0
                          • V VRonin
                            28 Oct 2016, 20:04

                            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 28 Oct 2016, 20:05 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 28 Oct 2016, 20:50 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
                              • V VRonin
                                28 Oct 2016, 20:04

                                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 28 Oct 2016, 20:55 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
                                • V VRonin
                                  28 Oct 2016, 19:40

                                  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 28 Oct 2016, 22:56 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

                                  1/16

                                  28 Oct 2016, 18:33

                                  • Login

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