0

I am trying to make a chess game. I have made two header files and their cpp files : Pieces.h and ChessBoard.h. I have included Pieces.h in ChessBoard.h and it's compiling fine. But I want to have a method in Pieces that requires ChessBoard as a parameter. So when I try to include ChessBoard.h in Pieces.h I get all weird errors. Can someone please guide me how to include ChessBoard.h in Pieces.h ?

Pieces.h:

#ifndef PIECES_H
#define PIECES_H
#include <string>
#include "ChessBoard.h"
using namespace std;

class Pieces{

protected:
    bool IsWhite;
    string name;
public:
    Pieces();
    ~Pieces();

    // needs to be overwritten by every sub-class
    virtual bool isValidMove(string initial,string final, ChessBoard& chessBoard) = 0;
    bool isWhite();
    void setIsWhite(bool IsWhite);  
    string getName();   
};

#endif

ChessBoard.h :

#ifndef CHESSBOARD_H
#define CHESSBOARD_H

#include "Pieces.h"
#include <map>
#include <string.h>

class ChessBoard
  {
        // board is a pointer to a 2 dimensional array representing board.
        // board[rank][file]
        // file : 0 b 7 (a b h) 
        std::map<std::string,Pieces*> board;
        std::map<std::string,Pieces*>::iterator boardIterator;

  public:
    ChessBoard();
    ~ChessBoard();
    void resetBoard();
    void submitMove(const char* fromSquare, const char* toSquare);
    Pieces *getPiece(string fromSquare);
    void checkValidColor(Pieces* tempPiece); // to check if the right player is making the move

};
#endif

errors:

ChessBoard.h:26: error: ‘Pieces’ was not declared in this scope
ChessBoard.h:26: error: template argument 2 is invalid
ChessBoard.h:26: error: template argument 4 is invalid
ChessBoard.h:27: error: expected ‘;’ before ‘boardIterator’
ChessBoard.h:54: error: ISO C++ forbids declaration of ‘Pieces’ with no type
ChessBoard.h:54: error: expected ‘;’ before ‘*’ token
ChessBoard.h:55: error: ‘Pieces’ has not been declared
starsplusplus
  • 1,232
  • 3
  • 19
  • 32
user2709885
  • 413
  • 2
  • 8
  • 16
  • Circular include - replace includes with forward declarations where possible. – Luchian Grigore Jan 16 '14 at 12:26
  • Why does `Pieces` need to know about the `ChessBoard`? Doesn't a `Piece` belong to a `ChessBoard`? Move `isValidMove` to the `ChessBoard`. – bblincoe Jan 16 '14 at 12:27
  • I've made a method isValidMove in Pieces that checks whether the called Piece can move in the given Board. So I need the Board to check that. – user2709885 Jan 16 '14 at 12:29
  • And after adding forward declarations as @LuchianGrigore said, get a look [here.](http://stackoverflow.com/questions/553682/when-to-use-forward-declaration) – VP. Jan 16 '14 at 12:30
  • can you show us the `isValidMove` method? – starsplusplus Jan 16 '14 at 12:32
  • I've actually not implemented isValidMove so far. But forward declaration works fine. Thank you so much guys. :) – user2709885 Jan 16 '14 at 12:38
  • It shouldn't be the Piece's responsibility to know whether a move is valid with regards to the board's layout. A piece only 'knows' which relative positions it can move to. The game engine and the board know if the move is valid according to the rules. By doing as you plan to, you are going to write a lot of duplicate code in each class inherited from Piece. – SirDarius Jan 16 '14 at 12:54

2 Answers2

1

This is cause due to something called circular dependency. circular dependency

the problem is that when your program starts compiling (lets assume chessboard.h start compiling first).
It sees directive to include pieces.h so it skips the rest of the code and moves to pieces.h
Here the compiler sees directive to include chessboard.h
but since you included a header guard it doesn't include chessboard.h for the second time.
It continues to compile the rest of the code in pieces.h
This means the class in chessboard.h has not been declared yet and it causes an error
best idea to avoid this is to forward declare the other class rather than including a header file . But you must note that you cannot create any objects of a forward declared class you can only make a pointer or a reference variable.

forward declaring means to declare the class before using it.

class ChessBoard;

class Pieces
{
  ChessBoard *obj;  // pointer object
  ChessBoard &chessBoard;
krish
  • 104
  • 1
  • 13
0

It is called redundant inclusion. When you include an H in both classes together (Pieces and Chessboards), C++ normally gives weird errors. It is a very common mistake when you start programming in C++.

First i suggest you to check if you really need each class to be included in the other. If you are really sure about that, then the way to fix it is to choose one of them and move the include to the cpp. Then add a predeclaration of the class in the h.

For example, if you choose ChessBoard to be changed:

#include <map>
#include <string.h>

class Pieces;

class ChessBoard
  {

In the ChessBoard cpp you will have your #include "Pieces.h"

The Pieces h and cpp stay the same.

Darkgaze
  • 2,280
  • 6
  • 36
  • 59