首页 > 解决方案 > 同一头文件中的未知类型名称

问题描述

我有以下 .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->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

但是,当我使用 makefile 运行整个程序时,会出现以下错误:(其中包括(显然),但在这种情况下,只有这些是相关的)

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);
      |            ^~~~~~~~~

我真的不明白这些错误的原因,因为“未知”数据类型就在头文件本身中????

非常感激任何的帮助 :)

标签: crecursionheader-filesforward-declaration

解决方案


你有递归插入标题

图.h

#ifndef graphHeader
#define graphHeader

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

roap.h

#ifndef ROAP
#define ROAP

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

似乎不需要在标题中包含整个标题,而是需要"roap.h"在标题中"graph.h"声明结构Labirinto"graph.h"

typedef struct Labirinto Labirinto;

同样在引用 typedef 名称之前,您需要声明它们。例如,在这个引用名称的 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;

推荐阅读