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. invalid use of "this" in non-static member function

invalid use of "this" in non-static member function

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 346 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.
  • I Offline
    I Offline
    iliahero
    wrote on 9 Sept 2024, 12:45 last edited by
    #1

    hi. i get this error but i dont know why
    this is my code :

    QString player = "X";
    int gdata[9], winner = 0;
    QMessageBox::StandardButton menu;

    void MainWindow::changepl(int btn){
    if(player == "X"){
    player = "O";
    ui->cplayer->setText("Current Player: O");
    gdata[btn] = 1;
    }
    else{
    player = "X";
    ui->cplayer->setText("Current Player: X");
    gdata[btn] = 2;
    }
    }

    void checkwin(){
    if((gdata[0] == gdata[1] && gdata[1] == gdata[2]) || (gdata[0] == gdata[3] && gdata[3] == gdata[6]) ||
    (gdata[0] == gdata[4] && gdata[4]== gdata[8])){
    winner = gdata[0];
    }
    else if((gdata[4] == gdata[1] && gdata[1] == gdata[7]) || (gdata[4] == gdata[3] && gdata[3] == gdata[5]) ||
    (gdata[4] == gdata[2] && gdata[2] == gdata[6])){
    winner = gdata[4];
    }
    else if((gdata[8] == gdata[5] && gdata[5] == gdata[2]) || (gdata[8] == gdata[7] && gdata[7] == gdata[6])){
    winner = gdata[8];
    }

    if(winner == 1){
        menu = QMessageBox::question(this, "Game Ended", ****here i get error
        "Player X winned the game! \n do you want to play again?", QMessageBox::Yes | QMessageBox::No);
    }
    else if(winner == 2){
        menu = QMessageBox::question(this, "Game Ended", ****here i get error
        "Player O winned the game! \n do you want to play again?", QMessageBox::Yes | QMessageBox::No);
    }
    

    }

    in lines * i get the error
    +(i use the qmessagebox before like this but i dont know why now i get this error)

    plese help!
    thank

    A J 2 Replies Last reply 9 Sept 2024, 13:40
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Sept 2024, 13:14 last edited by
      #2

      Please format your code with the code tags - currently noone can deceiver your code.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • I iliahero
        9 Sept 2024, 12:45

        hi. i get this error but i dont know why
        this is my code :

        QString player = "X";
        int gdata[9], winner = 0;
        QMessageBox::StandardButton menu;

        void MainWindow::changepl(int btn){
        if(player == "X"){
        player = "O";
        ui->cplayer->setText("Current Player: O");
        gdata[btn] = 1;
        }
        else{
        player = "X";
        ui->cplayer->setText("Current Player: X");
        gdata[btn] = 2;
        }
        }

        void checkwin(){
        if((gdata[0] == gdata[1] && gdata[1] == gdata[2]) || (gdata[0] == gdata[3] && gdata[3] == gdata[6]) ||
        (gdata[0] == gdata[4] && gdata[4]== gdata[8])){
        winner = gdata[0];
        }
        else if((gdata[4] == gdata[1] && gdata[1] == gdata[7]) || (gdata[4] == gdata[3] && gdata[3] == gdata[5]) ||
        (gdata[4] == gdata[2] && gdata[2] == gdata[6])){
        winner = gdata[4];
        }
        else if((gdata[8] == gdata[5] && gdata[5] == gdata[2]) || (gdata[8] == gdata[7] && gdata[7] == gdata[6])){
        winner = gdata[8];
        }

        if(winner == 1){
            menu = QMessageBox::question(this, "Game Ended", ****here i get error
            "Player X winned the game! \n do you want to play again?", QMessageBox::Yes | QMessageBox::No);
        }
        else if(winner == 2){
            menu = QMessageBox::question(this, "Game Ended", ****here i get error
            "Player O winned the game! \n do you want to play again?", QMessageBox::Yes | QMessageBox::No);
        }
        

        }

        in lines * i get the error
        +(i use the qmessagebox before like this but i dont know why now i get this error)

        plese help!
        thank

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 9 Sept 2024, 13:40 last edited by
        #3

        @iliahero said in invalid use of "this" in non-static member function:

        void checkwin(){

        void checkwin(){ is no member function therefore it is not bound to any object and therefore you have no this pointer.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • I iliahero
          9 Sept 2024, 12:45

          hi. i get this error but i dont know why
          this is my code :

          QString player = "X";
          int gdata[9], winner = 0;
          QMessageBox::StandardButton menu;

          void MainWindow::changepl(int btn){
          if(player == "X"){
          player = "O";
          ui->cplayer->setText("Current Player: O");
          gdata[btn] = 1;
          }
          else{
          player = "X";
          ui->cplayer->setText("Current Player: X");
          gdata[btn] = 2;
          }
          }

          void checkwin(){
          if((gdata[0] == gdata[1] && gdata[1] == gdata[2]) || (gdata[0] == gdata[3] && gdata[3] == gdata[6]) ||
          (gdata[0] == gdata[4] && gdata[4]== gdata[8])){
          winner = gdata[0];
          }
          else if((gdata[4] == gdata[1] && gdata[1] == gdata[7]) || (gdata[4] == gdata[3] && gdata[3] == gdata[5]) ||
          (gdata[4] == gdata[2] && gdata[2] == gdata[6])){
          winner = gdata[4];
          }
          else if((gdata[8] == gdata[5] && gdata[5] == gdata[2]) || (gdata[8] == gdata[7] && gdata[7] == gdata[6])){
          winner = gdata[8];
          }

          if(winner == 1){
              menu = QMessageBox::question(this, "Game Ended", ****here i get error
              "Player X winned the game! \n do you want to play again?", QMessageBox::Yes | QMessageBox::No);
          }
          else if(winner == 2){
              menu = QMessageBox::question(this, "Game Ended", ****here i get error
              "Player O winned the game! \n do you want to play again?", QMessageBox::Yes | QMessageBox::No);
          }
          

          }

          in lines * i get the error
          +(i use the qmessagebox before like this but i dont know why now i get this error)

          plese help!
          thank

          J Offline
          J Offline
          JonB
          wrote on 9 Sept 2024, 13:40 last edited by
          #4

          @iliahero
          As @Christian-Ehrlicher has said, please think about people trying to read your code and use the forum's Code tags (</> button).

          I presume you get the error on the this in both those lines. Since you appear (from what we can see) to have put that inside a global/free function, not inside a class, there is no this.

          1 Reply Last reply
          0
          • I iliahero has marked this topic as solved on 10 Sept 2024, 04:19
          • I Offline
            I Offline
            iliahero
            wrote on 10 Sept 2024, 04:20 last edited by
            #5

            thanks to all
            and im very sorry for not using forums code tags
            my problem solved!

            1 Reply Last reply
            1

            4/5

            9 Sept 2024, 13:40

            • Login

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