0

I am trying to make chess. In class figure I put a pointer to object square, that points at its "location" and vice versa: in square a pointer to figure. The problem is that definitions of these classes are in separate files. When I wrote #include "square.cpp" in figure.cpp and vice versa, compiler show many errors in the lines with #include, like:

from [path to figure] from [path to square]

Is there any way to make it work?

Hassan
  • 75
  • 6
  • 1
    Google `forward declaration`. Never ever have .h files that include each other. – n. m. could be an AI Aug 23 '15 at 09:19
  • 2
    Separate your code into .h and .cpp files, and include only .h files. – interjay Aug 23 '15 at 09:20
  • Off-Topic: I'm curious about your design. Why do Figure and Square know each other? – MABVT Aug 23 '15 at 09:21
  • @MABVT Inside Figure I have got virtual methods that determine if it can move. They need to know its coordinates of its actual and potential square, and if it is free or occupied by enemy. Square needs to see by whom is it possessed to allow its button interact whit it. – Hassan Aug 23 '15 at 10:21
  • You could - in further steps - refactor that logic out into a "ChessRuleService". That service knows all rules, the Figures, Squares and its relations. You could then do something like a "service.queryMovementAllowed(fig : Figure, action : Movement) : bool;". The button state can be updated by doing a "service.queryPermittedActions(sq : Square, fig : Figure) : List;". The Figure and Square-class use an "IChessRuleService"-interface (IChessRuleService.h), which eliminates the circular dependencies between Figure, Square and the ChessRuleService-imp. This is how i would do it. :D – MABVT Aug 23 '15 at 10:27
  • Keep me updated about your project! I'm interested in your progress – MABVT Aug 23 '15 at 10:27

2 Answers2

0

try adding include guards in the header:

#ifndef YOURHEADER_H_ //at the start of the file
#define YOURHEADER_H_
// the header contents
#endif //this has to be at the end of the file
user_4685247
  • 2,878
  • 2
  • 17
  • 43
0

First, notice that you could avoid any application #include (so only include system headers or headers for external libraries) and just copy & paste source code into several files. That would be inconvenient. Read more about the C preprocessor. Notice that it is mostly doing textual processing (e.g. does not know much about C++ syntax or ASTs).

Then, conventionally, header files contain mostly declarations (of types, struct, class, functions, variables, ...) and definitions of static inline functions (or inline member functions of your class-es).

So you'll better not #include "square.cpp" in your figure.cpp source file, but just have a single myheader.h file with a #include "myheader.h" both in your square.cpp and your figure.cpp source files (i.e. translation units).

Read more books about C++ programming and study the source code of some C++ free software, e.g. hun7err's Chess and ChessPlusPlus (I am not expert in chess programming, and found both of them thru quick google searches)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547