Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use Global Functions correctly? (Undefined reference)



  • Hello, i´ve tried to write a program for different sort algorithm.

    As soon as i tried to add a Function to Global im getting errors. Before the code worked fine. so the issue should be by one of the followings, but i dont know what causes the issue, but its always if the program try to open the (Ausgabe)Function in a Function:

    frmmain.cpp

    void Ausgabe(QVector<int> &ausgabe, QVector<int> &sort, QListWidget *lwSort, QListWidget *lwArray, int &anzahl, bool &sortierer);
    
    void Ausgabe(QVector<int> &array, QVector<int> &sort, QListWidget *lwSort, QListWidget *lwArray, int &anzahl, bool &sortierer)
    {
       if(sortierer == false)
       {
           for(int i=0; i <= anzahl-1; i++)
           {
              lwArray->addItem(QString::number(array[i]));
           }
       }
       else
       {
           for(int i=0; i <= anzahl-1; i++)
           {
              lwSort->addItem(QString::number(sort[i]));
           }
       }
       sortierer = false;
    }
    
    void FrmMain::on_btnBubble_clicked()
    {
        sortierer = true;
        BubbleSortASC(sort, tempVector, anzahl);
        Ausgabe(array, sort, ui->lwSort, ui->lwArray, anzahl, sortierer);
    }
    

    frmmain.h

    private slots:
        void on_btnArray_clicked();
    
        void on_cbSortiert_clicked();
    
        void on_cbOrderBy_clicked();
    
        void on_cbFirst_clicked();
    
        void Ausgabe(QVector<int> &array, QVector<int> &sort, QListWidget *lwSort, QListWidget *lwArray, int &anzahl, bool &sortierer);
    
        void on_btnBubble_clicked();
    
    private:
        Ui::FrmMain *ui;
        bool asc = false;
        bool desc = false;
        bool first = false;
        bool sortierer = false;
        QVector<int> tempVector;
        QVector<int> array;
        QVector<int> sort;
        int anzahl;
    };
    


  • You have a private slot member function with the same name ("Ausgabe") and the same parameters.
    When you call Ausgabe in FrmMain::on_btnBubble_clicked, the compiler will try to call FrmMain::Ausgabe(...) which I think you probably have not implemented...
    If I'm right, just delete the void Ausgabe(...) defined in frmmain.h.
    Little tip: If you have both a global function and a class member function with the same name and parameters, and you want to call the global function inside the class, then you need to add ::, for example: ::Ausgabe(...)


  • Qt Champions 2019

    @Symbiose Why do you have void Ausgabe(...) also in private slots?! In this case you have to prefix it with FrmMain:: as it is a method of the class...


  • Qt Champions 2019

    @Symbiose Also please show the complete error message: what is undefined?



  • You have a private slot member function with the same name ("Ausgabe") and the same parameters.
    When you call Ausgabe in FrmMain::on_btnBubble_clicked, the compiler will try to call FrmMain::Ausgabe(...) which I think you probably have not implemented...
    If I'm right, just delete the void Ausgabe(...) defined in frmmain.h.
    Little tip: If you have both a global function and a class member function with the same name and parameters, and you want to call the global function inside the class, then you need to add ::, for example: ::Ausgabe(...)



  • Thats it. I thought i would need to do that, but i only need to declare all Parameters Global. Thanks to everyone!


Log in to reply