c++ - Incomplete type error header -- how to fix? -


i thought had handle on how fix circular dependencies, can't fix following, i'd grateful help!

i writing chess program , have class pieces.

it's in file pieces.cpp , includes pieces.h.

pieces.h include rook.h.

rook.h include chesspiece.h

chesspiece.h include chessboard.h.

now,

chessboard.h include pieces.h.

i have include guards everywhere, goes right; except chessboard owns pieces rather having pointers/references them:

private:     pieces black_pieces;     pieces white_pieces; 

i did forward declaration of pieces, compiler complains incomplete type on line. can see why: @ point, compiler not know how space allocate class of type pieces, though knows 1 exists forward declaration.

my problem want chessboard store these pieces objects, cannot have pointers them. suppose put them on heap, i've heard using heap discouraged. case maybe useful, or there solution overlooking?

i hope explanation understandable without posting code -- there lot of it!

thanks help.

forward declarations work member references , pointers. when declaring instances of classes or structures in class / structure, need full declaration.

your question sounds have issue design / architecture.
rook is-a chess_piece. fine, sounds good.

the class pieces sounds ambiguous.
need specialized type containing pieces or can use std::vector<chess_piece*>?

a chessboard should contain 0 or more instances of chess_piece. common implementation 2d array of pointers chess_piece. pointers support polymorhphism.

another implementation vector of . of view chess board has pieces @ different locations on board.

edit 1: allocation of pieces
suggest having factory returns pointers pieces. since quantity of pieces known , won't change, can allocated static, automatic variables.

class piece_factory {   static std::vector<pawn> white_pawns;   static std::vector<pawn> black_pawns;   static queen             white_queen(white);   static queen             black_queen(black); public:   piece_factory()   {     pawn wp(white);     pawn bp(black);     (unsigned int = 0; < 8; ++i)     {       white_pawns.push_back(wp);  // append copy vector.       black_pawns.push_back(bp);     }   }   chess_piece * get_white_pawn(unsigned int index)   {     return &white_pawn[index];   }   chess_piece * get_white_queen(void)   {     return &white_queeen;   } }; 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -