#c #recursion #header-files #forward-declaration
Вопрос:
У меня есть следующие файлы .h (не обращайте внимания на комментарии):
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-gt;L)*(lab-gt;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 lt;stdio.hgt; #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); | ^~~~~~~~~
Я действительно не понимаю причину этих ошибок, так как «неизвестные» типы данных находятся прямо в самих заголовочных файлах????
Любая помощь была бы весьма признательна 🙂
Ответ №1:
У вас есть рекурсивная вставка заголовков
график.h
#ifndef graphHeader #define graphHeader #include "roap.h" #include "short.h" //...
и
роап.ч
#ifndef ROAP #define ROAP #include lt;stdio.hgt; #include "graph.h" #include "short.h" //...
Похоже, что вместо включения всего заголовка "roap.h"
в заголовок "graph.h"
вам нужно объявить структуру Labirinto
в заголовке "graph.h"
typedef struct Labirinto Labirinto;
Также перед тем, как ссылаться на имена типов, вам необходимо их объявить. Например, перед этим объявлением typedef, которое ссылается на имя 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;
тебе нужно написать
typedef struct vertex_adj Vertex_adj;
Ответ №2:
Используйте прямое объявление Vertex
:
typedef struct vertex Vertex; typedef struct graph { int V; //número de vértices = (lab-gt;L)*(lab-gt;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) };
В противном случае тип неизвестен в строке 12.
То же самое для 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;
Вместо:
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;