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 use Global Functions correctly? (Undefined reference)
QtWS25 Last Chance

How to use Global Functions correctly? (Undefined reference)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 353 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.
  • S Offline
    S Offline
    Symbiose
    wrote on last edited by Symbiose
    #1

    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;
    };
    
    jsulmJ 2 Replies Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #4

      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(...)

      1 Reply Last reply
      4
      • S Symbiose

        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;
        };
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

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

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

        1 Reply Last reply
        3
        • S Symbiose

          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;
          };
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

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

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

          1 Reply Last reply
          3
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #4

            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(...)

            1 Reply Last reply
            4
            • S Offline
              S Offline
              Symbiose
              wrote on last edited by
              #5

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

              1 Reply Last reply
              1

              • Login

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