1

I am making a chess program and I am having trouble getting the pieces to move when you click on them. I keep getting an error saying "friend declaration specifying a default argument must be a definition". Not sure what is wrong with my code.

This is my ChessGame.H file....

#ifndef CHESS_H
#define CHESS_H

#include <QMainWindow>
#include <QMouseEvent>
#include <QImage>
#include <QPaintDevice>

class Piece {
public:
    bool color;
    QImage image;
    void draw(int x, int y, QPainter* paint);
    Piece(bool c);
};

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

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

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

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

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

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

class Chess : public QMainWindow {
    Q_OBJECT

public:
    Chess(QWidget* parent = 0);
    ~Chess();
    void paintEvent(QPaintEvent*);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
};

#endif // CHESS_H

This is my ChessGame.cpp file...

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

    Piece* board[8][8];

Chess::Chess(QWidget* parent)
    : QMainWindow(parent)

{
    resize(44 * 8, 44 * 8);
    this->setWindowTitle("Chess");

    for (int y = 0; y < 8; y++) {
        for (int x = 0; x < 8; x++) {
            board[x][y] = 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[4][7] = new King(true);
    board[4][0] = new King(false);
    board[3][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 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("/Users/oxmem23xo/chess/chess_images/wrook.gif").toImage();
    }
    else if (color == false) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/brook.gif").toImage();
    }
}

Pawn::Pawn(bool c)
    : Piece(c)
{
    if (color == true) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/wpawn.gif").toImage();
    }
    else if (color == false) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/bpawn.gif").toImage();
    }
}

Knight::Knight(bool c)
    : Piece(c)
{
    if (color == true) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/bknight.gif")
                    .toImage();
    }
    else if (color == false) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/wknight.gif").toImage();
    }
}

Queen::Queen(bool c)
    : Piece(c)
{
    if (color == true) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/wqueen.gif").toImage();
    }

    else if (color == false) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/bqueen.gif").toImage();
    }
}

King::King(bool c)
    : Piece(c)
{
    if (color == true) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/wking.gif").toImage();
    }

    else if (color == false) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/bking.gif").toImage();
    }
}

Bishop::Bishop(bool c)
    : Piece(c)
{
    if (color == true) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/bbishop.gif")
                    .toImage();
    }

    else if (color == false) {
        image = QPixmap("/Users/oxmem23xo/chess/chess_images/wbishop.gif")
                    .toImage();
    }
}

Chess::~Chess()
{
}

void Chess::paintEvent(QPaintEvent*)
{

    QPainter paint(this);

    for (int x = 0; x < 8; ++x) {
        for (int y = 0; y < 8; ++y) {
            if ((x + y + 1) % 2 == 0) {
                paint.fillRect(x * 44, y * 44, 44, 44, QColor(177, 113, 24));
            }
            else {
                paint.fillRect(x * 44, y * 44, 44, 44, QColor(233, 174, 95));
            }
        }
    }

    for (int y = 0; y < 8; y++) {
        for (int x = 0; x < 8; x++) {
            if (board[x][y] != NULL) {
                board[x][y]->draw(x, y, &paint);
            }
        }
    }
}

void Chess::mousePressEvent(QMouseEvent* event)
{
    int startx = event->x() / 44;
    int starty = event->y() / 44;
}

void Chess::mouseReleaseEvent(QMouseEvent* event)
{

    int endx = event->x() / 44;
    int endy = event->y() / 44;
    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;
}
lexpro
  • 11
  • 3
  • The error does not seem to be in the section of code you provided, since it is about specifying default arguments in friend declarations. Look for parts of your code where you are using `friend`, see [this question](http://stackoverflow.com/q/23333949/2666212). – Mike Dec 16 '16 at 23:49
  • I'm not using friend anywhere in my code or at least not that I know of.... – lexpro Dec 17 '16 at 00:02
  • Declaring all the classes on one header file makes the classes friends: the issue is you are creating a lot of classes inheriting from Piece, but Piece itself was not implemented (it is implemented in the cpp file). This creates a problem. As a reference: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#136 – Luca Angioloni Dec 17 '16 at 03:09
  • what do I do to fix this? – lexpro Dec 18 '16 at 01:07
  • Can anyone help me more? Still trying to solve the problem! – lexpro Dec 19 '16 at 01:53
  • The compiler must issue the error message for a specific line of code. Show us that code! – Bo Persson Dec 19 '16 at 23:06
  • The error message is not appearing on any line of code it is only showing up in the issues box. – lexpro Dec 20 '16 at 04:08
  • could it be a problem with qt creator itself? I am running it on a mac do I need to update anything such as xcode? – lexpro Dec 20 '16 at 14:06

0 Answers0