Chess program with unknown error

Hi everyone I was making a chess program, and it keeps giving me this weird error that I'm not sure how to fix or what is really even wrong in the first place?
Heres the .h file:

<#ifndef CHESS_H
#define CHESS_H
#include <QImage>
#include <QMainWindow>
#include <QPaintDevice>

void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
void draw(int x, int y, QPainter* paint);
class Chess : public QMainWindow
{
Q_OBJECT
void paintEvent(QPaintEvent*);

public:
Chess(QWidget *parent = 0);
~Chess();
};


class Piece
{
public:
bool color;
QImage image;
Piece(bool c)
{

}
};

class Rook:Piece
{
public:
Rook(bool c)
{

}
};
class Pawn:Piece
{
public:
Pawn(bool c)
{ //the error happens here on every different piece subclass.

}
};
class Bishop:Piece
{
public:
Bishop(bool c)
{

}
};
class King:Piece
{
public:
King(bool c)
{

}
};
class Queen:Piece
{
public:
Queen(bool c)
{

}
};
class Knight:Piece
{
public:
Knight(bool c)
{

}
};

#endif // CHESS_H>



and here's the chess.cpp file:

<#include "chess.h"
#include <QPainter>

Piece* board[7][7];
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][0]=new Bishop(true);
board[5][0]=new Bishop(true);
board[2][7]=new Bishop(false);
board[5][7]=new Bishop(false);
board[1][0]=new Knight(true);
board[6][0]=new Knight(true);
board[1][7]=new Knight(false);
board[6][7]=new Knight(false);

void Chess::mousePressEvent(QMouseEvent* event)
{
static int startx;
static int starty;
xcoord=event->x() / 44;
ycoord=event->y() / 44;
}
void Chess::mouseReleaseEvent(QMouseEvent* event)
{
int endx, endy;
endx=startx;
endy=starty;
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(this);
event->x() / 44;
event->y() / 44;
}

}
Piece::Piece(bool c)
{
bool c;
color=c;
void Piece::draw(int x, int y, QPainter* paint)
{
paint->drawImage(0*44,7*44,image);
}
}
Rook::Rook(bool c):Piece(c)
{
if(color==true)
{
image=QPixmap("wrook.gif").toImage();
}
else if(color==false)
{
image=QPixmap("brook.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;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 haven't modified the .pro, or main.cpp file at all. The error message is saying "no matching function for call to 'Piece::Piece()' ". It's happening in the spot I commented in the code in the .h file. Thanks for any help!
closed account (E0p9LyTq)
That error is going to happen with every derived class, you are not initializing the derived classes' base class properly.

Either create a default Piece class constructor (Piece() { color = false; }) or initialize the base class in each derived class:

1
2
3
4
5
6
7
class Rook : Piece
{
public:
   Rook(bool c) : Piece(c)
   {
   }
};


PLEASE learn to use code tags, it makes reading your code much easier, and puts line numbers on the code for easier documentation.

You can go back and add code tags to your post.

http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Topic archived. No new replies allowed.