Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to use Global Functions correctly? (Undefined reference)

    General and Desktop
    3
    5
    67
    Loading More Posts
    • 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
      Symbiose last edited by 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;
      };
      
      jsulm 2 Replies Last reply Reply Quote 0
      • B
        Bonnie last edited by Bonnie

        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 Reply Quote 4
        • jsulm
          jsulm Lifetime Qt Champion @Symbiose last edited by jsulm

          @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 Reply Quote 3
          • jsulm
            jsulm Lifetime Qt Champion @Symbiose last edited by

            @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 Reply Quote 3
            • B
              Bonnie last edited by Bonnie

              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 Reply Quote 4
              • S
                Symbiose last edited by

                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 Reply Quote 1
                • First post
                  Last post