Arrays of functions help



  • 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;
    
    
    }
    
    


  • 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);
    }
    


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



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



  • @VRonin Yes i included that.

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



  • 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!


  • Moderators

    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(); };
        }
    }
    


  • @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.



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



  • @VRonin yes I have done that several times.



  • 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;
    }
    


  • 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;
    }
    

  • Moderators

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


  • Lifetime Qt Champion

    @Wieland Starwars on Super 8mm ;)

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


  • Qt Champions 2016

    @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. ;)


  • Moderators

    @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();});
    }
    

Log in to reply
 

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