invalid use of "this" in non-static member function
-
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 -
Please format your code with the code tags - currently noone can deceiver your code.
-
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@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 nothis
pointer.Regards
-
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@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 nothis
. -