指定默认参数的friend声明必须是定义错误

时间:2016-12-16 23:18:15

标签: c++ qt mouseevent friend chess

我正在制作一个国际象棋程序,当你点击它们时我无法移动棋子。我一直收到错误说"指定默认参数的朋友声明必须是定义"。不确定我的代码有什么问题。

这是我的ChessGame.H文件......

#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

这是我的ChessGame.cpp文件...

#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;
}

0 个答案:

没有答案