0

I'm working om a simple program that creates rooms in a house according to a map. For every room, i list the connected rooms. The thing is, this cannot compile. Any idea why ?

typedef struct s_room {
    char *name;
    int nbr;
    int x;
    int y;
    bool start;
    bool end;
    int ants;
    int current;
    t_room *connect;
    int visited;

} t_room;

I think it comes from the t_room *connect, but I can't figure out how to solve this.

ajay
  • 9,402
  • 8
  • 44
  • 71
Pierre Olivier Tran
  • 1,669
  • 4
  • 24
  • 40

3 Answers3

4

Replace

typedef struct      s_room
{
    ....
    t_room          *connect;
    ....
}                   t_room;

with

typedef struct      s_room
{
    ....
    struct s_room   *connect;
    ....
}                   t_room;
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
3

Two options:

  1. Forward declaration of type

    typedef struct      s_room t_room;
    struct s_room {
        t_room          *connect;
    };
    
  2. Use of struct s_room

    typedef struct s_room {
        struct s_room  *connect;
    } t_room;
    
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
1

You can't use the structure type t_room to define the type t_room itself because it is not yet defined. Therefore you should replace this

t_room *connect;

with

struct s_room *connect;

Or you can typedef struct s_room type before actually defining struct s_room. Then you can use the type alias in the definition.

// struct s_room type is not defined yet.
// create an alias s_room_t for the type.

typedef struct s_room s_room_t; 

// define the struct s_room type and
// create yet another alias t_room for it.

typedef struct s_room {
    // other data members
    s_room_t *connect; 

    // note that type of connect is not yet defined
    // because struct s_room of which s_room_t is an alias
    // is not yet fully defined.

} t_room;

// s_room_t and t_room are both aliases for the type struct s_room

Personally, I'd prefer the former because to do the latter, you have to introduce one extra typedef just to define the other typedef! This looks like namespace clutter without any real benefit.

ajay
  • 9,402
  • 8
  • 44
  • 71