The expected class name before the '{' token

I get the error "expected class name before" {"token" in my C ++ Qt project. After he went into the game, it seems that the problem with the circular inclusion. I have pawn.h, which includes a piece of .h, which includes board.h, which completes the circle, including pawn.h. I read that this can be fixed using forward declarations, but I tried to scroll through several classes of problems, and that would not work.

#ifndef PAWN_H #define PAWN_H #include "piece.h" class Pawn : public Piece { Q_OBJECT public: explicit Pawn(QWidget *parent = 0); }; #endif // PAWN_H 

.

 #ifndef PIECE_H #define PIECE_H #include <QWidget> #include "board.h" class Board; class Piece : public QWidget { Q_OBJECT public: explicit Piece(QWidget *parent = 0); void setPosition(int rank, int file); QPixmap pixmap; protected: void paintEvent(QPaintEvent *); private: int rank; int file; int x; int y; }; #endif // PIECE_H 

.

 #ifndef BOARD_H #define BOARD_H #include <QWidget> #include <QVector> #include <QGridLayout> #include "square.h" #include "pawn.h" #include "knight.h" #include "bishop.h" #include "queen.h" #include "king.h" class Board : public QWidget { public: explicit Board(QWidget *parent = 0); QVector < QVector<Square *> > sqrVector; Pawn *pawn[8]; Knight *knight[2]; Bishop *bishop[2]; Queen *queen; King *king; private: QGridLayout *layout; }; #endif // BOARD_H 
+4
source share
2 answers

Instead of random attempts, try modifying board.h to include forward declarations for all parts:

board.h

 class Pawn; class Knight; class Bishop; class Queen; class King; 

And remove the corresponding #include statements. (You will probably need to put these #include statements in board.cpp when you decide what you need to see inside the various piecewise classes.)

+4
source

Your main problem lies in the file: piece.h . Since board not explicitly specified in the file, include for it and for direct declaration should be deleted. It will break the circle. In addition, as Greg pointed out , only advanced declarations are needed in board.h .

+1
source

Source: https://habr.com/ru/post/1369091/


All Articles