Chess program issues

Hi everyone, I've been writing this chess program for a class with QT Creator, and I have this error that's telling me I'm not defining a function, even though I thought I did. The error is in the .cpp file, and the function definition is in the .h file. They are as follows:
.h
[#ifndef CHESS_H
#define CHESS_H
#include <QImage>
#include <QMainWindow>
#include <QPaintDevice>
#include <QMouseEvent>


class Chess : public QMainWindow
{
Q_OBJECT
void paintEvent(QPaintEvent*);
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);

public:
Chess(QWidget *parent = 0);
~Chess();
};
class Piece
{
public:
bool color;
QImage image;
void draw(int x, int y, QPainter* paint);
virtual bool canMove(int startx, int starty, int endx, int endy)=0;
Piece(bool c);
};
class Rook: public Piece
{
public:
Rook(bool c);
bool canMove(int startx, int starty, int endx, int endy)
{
return false;
}
};
class Pawn:public Piece
{
public:
Pawn(bool c);
bool canMove(int startx, int starty, int endx, int endy)
{
return false;
}
};
class Bishop:public Piece
{
public:
Bishop(bool c);
bool canMove(int startx, int starty, int endx, int endy)
{
return false;
}
};
class King:public Piece
{
public:
King(bool c);
bool canMove(int startx, int starty, int endx, int endy)
{
return false;
}
};
class Queen:public Piece
{
public:
Queen(bool c);
bool canMove(int startx, int starty, int endx, int endy)
{
return false;
}
};
class Knight:public Piece
{
public:
Knight(bool c);
bool canMove(int startx, int starty, int endx, int endy)
{
return false;
}
};
#endif // CHESS_H]



.cpp:
[#include "chess.h"
#include <QPainter>
#include <QMouseEvent>
Piece* board[8][8];
int startx;
int starty;
Chess::Chess(QWidget *parent)
: QMainWindow(parent)
{
int x=0, y=0;
resize(44*8,44*8);
for(y;y<8;y++)
{
x=0;
for(x;x<8;x++)
{
board[y][x]=NULL;
}
}
board[0][0]=new Rook(false);
board[7][0]=new Rook(false);
board[0][7]=new Rook(true);
board[7][7]=new Rook(true);
board[0][6]=new Pawn(true);
board[1][6]=new Pawn(true);
board[2][6]=new Pawn(true);
board[3][6]=new Pawn(true);
board[4][6]=new Pawn(true);
board[5][6]=new Pawn(true);
board[6][6]=new Pawn(true);
board[7][6]=new Pawn(true);
board[0][1]=new Pawn(false);
board[1][1]=new Pawn(false);
board[2][1]=new Pawn(false);
board[3][1]=new Pawn(false);
board[4][1]=new Pawn(false);
board[5][1]=new Pawn(false);
board[6][1]=new Pawn(false);
board[7][1]=new Pawn(false);
board[3][7]=new King(true);
board[4][0]=new King(false);
board[4][7]=new Queen(true);
board[3][0]=new Queen(false);
board[2][7]=new Bishop(true);
board[5][7]=new Bishop(true);
board[2][0]=new Bishop(false);
board[5][0]=new Bishop(false);
board[1][7]=new Knight(true);
board[6][7]=new Knight(true);
board[1][0]=new Knight(false);
board[6][0]=new Knight(false);


}
void Chess::mousePressEvent(QMouseEvent* event)
{
startx=event->x() / 44;
starty=event->y() / 44;
}
void Chess::mouseReleaseEvent(QMouseEvent* event)
{
int endx, endy;
endx=event->x() / 44;
endy=event->y() / 44;
if(canMove(startx,starty,endx,endy)==true) //this is where the error is. It says canMove is not defined in this scope***********
{
if((startx<0 || startx>7) || (starty<0 || starty>7) || (endx<0 || endx>7) || (endy<0 || endy>7) || (board[startx][starty]==NULL))
{
return;
}
board[endx][endy]=board[startx][starty];
board[startx][starty]=NULL;
repaint();
}
}
void Piece::draw(int x, int y, QPainter* paint)
{
paint->drawImage(x*44,y*44,image);
}
Piece::Piece(bool c)
{
color=c;
}
Rook::Rook(bool c):Piece(c)
{
if(color==true)
{
image=QPixmap("wrook.gif").toImage();
}
else if(color==false)
{
image=QPixmap("brook.gif").toImage();
}
}
Pawn::Pawn(bool c):Piece(c)
{
if(color==true)
{
image=QPixmap("wpawn.gif").toImage();
}
else if(color==false)
{
image=QPixmap("bpawn.gif").toImage();
}
}
Knight::Knight(bool c):Piece(c)
{
if(color==true)
{
image=QPixmap("wknight.gif").toImage();
}
else if(color==false)
{
image=QPixmap("bknight.gif").toImage();
}
}
Queen::Queen(bool c):Piece(c)
{
if(color==true)
{
image=QPixmap("wqueen.gif").toImage();
}
else if(color==false)
{
image=QPixmap("bqueen.gif").toImage();
}
}
King::King(bool c):Piece(c)
{
if(color==true)
{
image=QPixmap("wking.gif").toImage();
}
else if(color==false)
{
image=QPixmap("bking.gif").toImage();
}
}
Bishop::Bishop(bool c):Piece(c)
{
if(color==true)
{
image=QPixmap("wbishop.gif").toImage();
}
else if(color==false)
{
image=QPixmap("bbishop.gif").toImage();
}
}
void Chess::paintEvent(QPaintEvent*)
{
int x=0, y=0;
QPainter paint(this);
for(y;y<8;y++)
{
x=0;
for(x;x<8;x++)
{
if(y%2==0)
{
if(x%2==0 && y%2==0)
{
paint.fillRect(x*44,y*44,44,44,QColor(177,113,24));
}
else if(x%2==1 && y%2==0)
{
paint.fillRect(x*44,y*44,44,44,QColor(233,174,95));
}
}
else if(y%2==1)
{
if(x%2==0 && y%2==1)
{
paint.fillRect(x*44,y*44,44,44,QColor(233,174,95));
}
else if(x%2==1 && y%2==1)
{
paint.fillRect(x*44,y*44,44,44,QColor(177,113,24));
}
}
}
}
for(y=0;y<8;y++)
{
x=0;
for(x;x<8;x++)
{
if(board[x][y]!=NULL)
{
board[x][y]->draw(x,y,&paint);
}
}
}
}
Chess::~Chess()
{
}]


I havn't changed the main.cpp file or the .pro file so I doubt either of those are causing any issues. Any help is greatly appreciated!
P.S: Sorry if the code is a bit hard to read as I have literally no idea how to put it up hear in the original format with proper indentation. Sorry!
Last edited on
closed account (D80DSL3A)
mouseReleaseEvent is a Chess class member function, not a Piece class member function.
What Piece is calling canMove?
I have literally no idea how to put it up hear in the original format with proper indentation. Sorry!


It's really not difficult. See those buttons on the right when you're editing or writing a post? See the one that looks like this: <> ?

Highlight your code. Click that button.
Last edited on
Topic archived. No new replies allowed.