Home > OS >  unknown type name within same header file
unknown type name within same header file

Time:11-09

I have the following .h files (nevermind the comments):

graph.h

#ifndef graphHeader
#define graphHeader

#include "roap.h"
#include "short.h"

//estrutura que representa o grafo
typedef struct graph
{
    int V; //número de vértices = (lab->L)*(lab->C)
    int E; //número de arestas, com valor máximo (V(V-1))/2;
/*line 12*/ Vertex** adj; //aponta para um vetor de structs vertex
} Graph;

//estrutura que representa o vértice (casa) associado a cada adj[i]
typedef struct vertex
{
    int Lx; //coordenada em linhas do vértice (casa) em estudo
    int Cx; //idem para colunas
/*line 20*/ Vertex_adj* lista_h; //head da lista de adjacências propriamente dita das casas adjacentes, com, no máximo, 4 elemetos (norte sul este e oeste)
} Vertex;

//estrtura que representa cada uma das casas adjacentes 
typedef struct vertex_adj
{
    int Lx_adj; //= Lx 1 ou Lx-1
    int Cx_adj; //idem para Cx
    int custex; //custo da casa adjacente, ou seja, o custo de ir do vértice (Lx, Cx) para este
/*line 29*/ Vertex_adj* next_adj;
} Vertex_adj;


Graph* cria_graph(Graph* g);
Vertex** cria_vetor_structs(Vertex** ptr);
/*line 35*/Vertex_adj* cria_Lx_plus(Labirinto* m, int a);
/*line 36*/Vertex_adj* cria_Lx_less(Labirinto* m, int a);
/*line 37*/Vertex_adj* cria_Cx_plus(Labirinto* m, int a);
/*line 38*/Vertex_adj* cria_Cx_less(Labirinto* m, int a);
/*line 39*/void cria_lista_adj(Labirinto* m, Vertex_adj* lx_plus, Vertex_adj* lx_less, Vertex_adj* cx_plus, Vertex_adj* cx_less);
/*line 40*/void inicializa_array(Labirinto* m);
/*line 41*/LabList* junta_tudo(LabList* head);
/*line 42*/void free_everything(LabList* head);
/*line 43*/void print(Labirinto* m);

#endif

roap.h

#ifndef ROAP
#define ROAP

#include <stdio.h>
#include "graph.h"
#include "short.h"

//#define DEBUG      1
#define BUFFERSIZE 5

typedef struct Labirinto {
    /* dimensao do labirinto ints L e C */
    /* ints Lt e Ct das coordenadas do ponto de chegada */
    /* int P celulas negras/cinzentas */
    int L, C, cel_L, cel_C, P;
   // int modo;           // 1..6
    //int cel_2_L, cel_2_C;         // same room test coords
    int **tabuleiro;
    Graph* grafo;
} Labirinto;

/* Strictly has just one maze, however it also iterates the maze list */
typedef struct LabList {
    Labirinto* lab;
    struct LabList* next;
} LabList;

/* Get a single Maze from a file */
Labirinto* inputLab(FILE* filePtr);

/* Terminates if allocation failled */
void checkAllocationError(const void* ptr, const char* errorMsg);

void alloc_tabuleiro(Labirinto *);
void free_tabuleiro(Labirinto *);

LabList *criar_No_Lab (FILE *);
LabList *insert_in_list (LabList *, LabList *);
void free_lista(LabList *);
void print_tabuleiro(Labirinto *);

#endif

However, when I run the whole program with a makefile I get the following errors: (amongst others (obviously), but in this context only these are relevant)

graph.h:12:2: error: unknown type name ‘Vertex’
   12 |  Vertex** adj; //aponta para um vetor de structs vertex
      |  ^~~~~~
graph.h:20:2: error: unknown type name ‘Vertex_adj’
   20 |  Vertex_adj* lista_h; //head da lista de adjacncias propriamente dita das casas adjacentes, com, no mximo, 4 elemetos (norte sul este e oeste)
      |  ^~~~~~~~~~
In file included from roap.h:8,
                 from roap.c:8:
graph.h:29:2: error: unknown type name ‘Vertex_adj’
   29 |  Vertex_adj* next_adj;
      |  ^~~~~~~~~~
graph.h:35:26: error: unknown type name ‘Labirinto’
   35 | Vertex_adj* cria_Lx_plus(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:36:26: error: unknown type name ‘Labirinto’
   36 | Vertex_adj* cria_Lx_less(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:37:26: error: unknown type name ‘Labirinto’
   37 | Vertex_adj* cria_Cx_plus(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:38:26: error: unknown type name ‘Labirinto’
   38 | Vertex_adj* cria_Cx_less(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:39:21: error: unknown type name ‘Labirinto’
   39 | void cria_lista_adj(Labirinto* m, Vertex_adj* lx_plus, Vertex_adj* lx_less, Vertex_adj* cx_plus, Vertex_adj* cx_less);
      |                     ^~~~~~~~~
graph.h:40:23: error: unknown type name ‘Labirinto’
   40 | void inicializa_array(Labirinto* m);
      |                       ^~~~~~~~~
graph.h:41:1: error: unknown type name ‘LabList’
   41 | LabList* junta_tudo(LabList* head);
      | ^~~~~~~
graph.h:41:21: error: unknown type name ‘LabList’
   41 | LabList* junta_tudo(LabList* head);
      |                     ^~~~~~~
graph.h:42:22: error: unknown type name ‘LabList’
   42 | void free_everything(LabList* head);
      |                      ^~~~~~~
graph.h:43:12: error: unknown type name ‘Labirinto’
   43 | void print(Labirinto* m);
      |            ^~~~~~~~~
In file included from roap.h:8,
                 from short.h:8,
                 from short.c:4:
graph.h:12:2: error: unknown type name ‘Vertex’
   12 |  Vertex** adj; //aponta para um vetor de structs vertex
      |  ^~~~~~
graph.h:20:2: error: unknown type name ‘Vertex_adj’
   20 |  Vertex_adj* lista_h; //head da lista de adjacncias propriamente dita das casas adjacentes, com, no mximo, 4 elemetos (norte sul este e oeste)
      |  ^~~~~~~~~~
In file included from roap.h:8,
                 from short.h:8,
                 from short.c:4:
graph.h:29:2: error: unknown type name ‘Vertex_adj’
   29 |  Vertex_adj* next_adj;
      |  ^~~~~~~~~~
graph.h:35:26: error: unknown type name ‘Labirinto’
   35 | Vertex_adj* cria_Lx_plus(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:36:26: error: unknown type name ‘Labirinto’
   36 | Vertex_adj* cria_Lx_less(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:37:26: error: unknown type name ‘Labirinto’
   37 | Vertex_adj* cria_Cx_plus(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:38:26: error: unknown type name ‘Labirinto’
   38 | Vertex_adj* cria_Cx_less(Labirinto* m, int a);
      |                          ^~~~~~~~~
graph.h:39:21: error: unknown type name ‘Labirinto’
   39 | void cria_lista_adj(Labirinto* m, Vertex_adj* lx_plus, Vertex_adj* lx_less, Vertex_adj* cx_plus, Vertex_adj* cx_less);
      |                     ^~~~~~~~~
graph.h:40:23: error: unknown type name ‘Labirinto’
   40 | void inicializa_array(Labirinto* m);
      |                       ^~~~~~~~~
graph.h:41:1: error: unknown type name ‘LabList’
   41 | LabList* junta_tudo(LabList* head);
      | ^~~~~~~
graph.h:41:21: error: unknown type name ‘LabList’
   41 | LabList* junta_tudo(LabList* head);
      |                     ^~~~~~~
graph.h:42:22: error: unknown type name ‘LabList’
   42 | void free_everything(LabList* head);
      |                      ^~~~~~~
graph.h:43:12: error: unknown type name ‘Labirinto’
   43 | void print(Labirinto* m);
      |            ^~~~~~~~~

I really do not understand the reason of these error, as the "unknown" data types are right there in the header files themselves????

Any help would be most appreciated :)

CodePudding user response:

Use a forward declaration of Vertex:

typedef struct vertex Vertex;

typedef struct graph
{
    int V; //número de vértices = (lab->L)*(lab->C)
    int E; //número de arestas, com valor máximo (V(V-1))/2;
/*line 12*/ Vertex** adj; //aponta para um vetor de structs vertex
} Graph;

//estrutura que representa o vértice (casa) associado a cada adj[i]
struct vertex
{
    int Lx; //coordenada em linhas do vértice (casa) em estudo
    int Cx; //idem para colunas
/*line 20*/ Vertex_adj* lista_h; //head da lista de adjacências propriamente dita das casas adjacentes, com, no máximo, 4 elemetos (norte sul este e oeste)
};

Otherwise the type is unknown at line 12.

Same for Vertex_adj:

typedef struct vertex_adj
{
    int Lx_adj; //= Lx 1 ou Lx-1
    int Cx_adj; //idem para Cx
    int custex; //custo da casa adjacente, ou seja, o custo de ir do vértice (Lx, Cx) para este
/*line 29*/ Vertex_adj* next_adj;
} Vertex_adj;

Instead:

typedef struct vertex_adj
{
    int Lx_adj; //= Lx 1 ou Lx-1
    int Cx_adj; //idem para Cx
    int custex; //custo da casa adjacente, ou seja, o custo de ir do vértice (Lx, Cx) para este
    struct vertex_adj* next_adj;
} Vertex_adj;

CodePudding user response:

You have recursive insertion of headers

graph.h

#ifndef graphHeader
#define graphHeader

#include "roap.h"
#include "short.h"
//...

and

roap.h

#ifndef ROAP
#define ROAP

#include <stdio.h>
#include "graph.h"
#include "short.h"
//...

It seems that instead of including the whole header "roap.h" in the header "graph.h" you need to declare the structure Labirinto in the header "graph.h"

typedef struct Labirinto Labirinto;

Also before referring typedef names you need to declare them. For example before this typedef declaration that refers the name Vertex_adj

typedef struct vertex
{
    int Lx; //coordenada em linhas do vértice (casa) em estudo
    int Cx; //idem para colunas
    Vertex_adj* lista_h; 
} Vertex;

you need to write

typedef struct vertex_adj  Vertex_adj;
  • Related