-1

I have spent the last hour looking at threads with the same error, but not exactly the same problem, so I decided to ask myself. I am working on part of a project for school. It is a game that has multiple header files and cpp files. I just converted the files to cpp files from c files, and I am working on changing structs to classes (it is required to do so for the assignment).

I am getting the following error: error

Here is my character.h file:

#ifndef CHARACTER_H
# define CHARACTER_H

# include <stdint.h>

# include "dims.h"
# include "dungeon.h"

class character_t {
 public:
  char symbol;
  pair_t position;
  int32_t speed;
  uint32_t alive;
  uint32_t sequence_number;
  uint32_t kills[num_kill_types];
};

int32_t compare_characters_by_next_turn(const void *character1, const void *character2);
uint32_t can_see(dungeon_t *d, character_t *voyeur, character_t *exhibitionist);
void character_delete(void *c);

#endif

Here is my dungeon.h file:

#ifndef DUNGEON_H
# define DUNGEON_H

# include "heap.h"
# include "macros.h"
# include "dims.h"
# include "character.h"

class dungeon_t
{
 public:
  uint32_t num_rooms;
  room_t *rooms;
  terrain_type_t map[DUNGEON_Y][DUNGEON_X];
  uint8_t hardness[DUNGEON_Y][DUNGEON_X];
  uint8_t pc_distance[DUNGEON_Y][DUNGEON_X];
  uint8_t pc_tunnel[DUNGEON_Y][DUNGEON_X];
  character_t *character[DUNGEON_Y][DUNGEON_X];
  character_t pc;
  heap_t events;
  uint16_t num_monsters;
  uint16_t max_monsters;
  uint32_t character_sequence_number;                                  
  uint32_t time;
  uint32_t quit;
};

#endif

I know it is a lot to look at, but any idea what is causing this? I am obviously including the dungeon.h header. I have seen mention of circular header inclusions, but I do not understand how to avoid this when both headers need each other.

Edit: I appreciate the help. Your answers were more helpful than an hour or so of searching myself. You fixed my problem before I was even able to trim the content of the message down.

  • It is a lot to look at. You could help by reducing this to a [mcve]. Simply remove the parts that aren't relevant, until you're left with the minimal complete code that reproduces the error – Tas Oct 26 '17 at 04:25
  • 4
    You have circular includes. – user0042 Oct 26 '17 at 04:26
  • `dungeon.h` is included in `character.h` and `character.h` is included in `dungeon.h`. – H.S. Oct 26 '17 at 04:28
  • You avoid them by doing a forward declaration (basically saying “this class exists, I’ll tell you about it later”) and then including the header after the one using it. You’ll find information about it with a web search and I’m sure SO also has a duplicate of this – Sami Kuhmonen Oct 26 '17 at 04:28

1 Answers1

1

You have a problem with recursive header includes: dungeon.h includes character.h while character.h includes dungeon.h. To solve it you can write a header with a forward declaration of dungeon_t and include it into character.h instead.

user7860670
  • 35,849
  • 4
  • 58
  • 84