friend declaration specifying a default argument must be a definition error

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.




//chess.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











//chess.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;
}
The code snippets you posted do not have any friend declarations, definitions. But to declare friends with default arguments see below:
http://stackoverflow.com/questions/23333949/the-correct-way-to-define-default-argument-for-a-friend-function-in-c?noredirect=1&lq=1
edit: and please use code-tags if you're posting again
Last edited on
Is there any way to make the code work without using friend?
On which line(s) do you get the error message(s). What is the exact text of the error message in full.
It does not state the line where the error is, it just says in the error box this...
friend declaration specifying a default argument must be a definition
friend Q_CORE_EXPORT uint qHash(const Qurl &url, uint seed=0)
Q_DECL_NOTHROW;
^
Last edited on
I'm out of my depth here, this is software which I don't use.

However, a bit of googling found a bug report
https://bugreports.qt.io/browse/QTBUG-32100

and it was stated that the issue was closed in 2013 - that is, the code has been corrected.

Perhaps you have some outdated version of the code which you are using.
Last edited on
what do I do to fix this problem now? Any idea?
Well, did you check the version/date of the Qt software you are using?
The fix would appear to be to upgrade to the latest version.
I am using version 4.1.0 but I am running Qt creator on a mac maybe I should update xcode?
At this stage I don't think I'm able to help. You need assistance from other mac users of Qt creator. I don't qualify on either hardware or software grounds.
can anyone tell me if this is a problem with qt creator itself or with my code?
It is a not a problem in your code - the Qt library is broken.

Version 4.1.0 is fairly old; consider upgrading to a later (ideally the current) version of Qt.
how do I update? Do I need to uninstall qt creator and reinstall it?
Last edited on
> Do I need to uninstall qt creator and reinstall it?

I would presume that uninstalling the current version and then installing a later version should work.
http://macappstore.org/qt-creator/

I know nothing about qt; perhaps you would need to uninstall/reinstall the Qt library too.
http://macappstore.org/qt/
closed account (48T7M4Gy)
@OP Why not save yourself a whole lot of trouble and just go back to a running version of the program and work from there to replicate the problem. Chances are there will be one additional line that breaks the camels back.

Notwithstanding the unlikely possibility its a Qt version problem, I'd say the problem is elsewhere. So far it is just shooting in the dark.

Qt Creator will provide a reasonably easy to understand error code.

Is this you because the comments there might assist too?
http://stackoverflow.com/questions/41193759/friend-declaration-specifying-a-default-argument-must-be-a-definition-error

http://stackoverflow.com/questions/23333949/the-correct-way-to-define-default-argument-for-a-friend-function-in-c
Last edited on
Topic archived. No new replies allowed.