c++ - When I link two header files together, one does not recognize the other -


i'm making board game using c++ involves multiple classes. when include header file of piece.h in board.h, of piece's members being recognized board. when simultaneously link board.h piece.h, members of board not being recognized. here how linking them:

-in piece.h

    #ifndef piece_h_     #define piece_h_     #include <iostream>     #include "board.h" 

-in board.h

    #ifndef board_h_     #define board_h_     #include<iostream>     #include "piece.h" 

i declared multiple piece members of board , piece type parameters of board functions, , work fine. yet declaring void function within piece takes board parameters, such:

    void horizontal         (board b);     void vertical           (board b);     void diagonal           (board b); 

results in error saying "board has not been declared"

including file tells preprocessor "copy file's content here". if both headers refer each other, have circular reference, cannot compile. compiler has know @ least bit data types being used.
can done using forward declarations:

board.h

class piece; // forward declaration without defining type  class board {     // board can makes use of piece, long     // not need know it.     // references , pointers ok.     piece* foo();     void bar(piece&); }; 

piece.h

#include "board.h"  class piece {     // actual definition of class.     // @ point board defined , piece can make use of it.     board* foo() { /*...*/ }     void bar(board& x) { /*...*/ }      // not references possible:     board baz(const board x) { /*...*/ }  }; 

board.cpp

#include "board.h" #include "piece.h"  // implementation of board's functions can go after piece's definition: piece* board::foo() { /*...*/ } void board::bar(piece& x) { /*...*/ } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -