我有一个程序正在c中处理列表,只要我在一个源文件中有它,它就可以正常工作,当我尝试将其分离并编译时出现此错误“ delete_functions.c:15:13:错误:未知类型名称'nodetype'”,function_functions.c和insert_functions.c的错误相同,这是代码
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "delete_functions.h"
#include "insert_functions.h"
#include "functionality_functions.h"
int main(){
//i did not upload all the main function code because it is way to long
}
types.h
typedef char AirportCode[4];
typedef struct nodetype{
char Airport[4];
struct nodetype *next;
} nodetype;
delete_functions.h
void Delete(nodetype *list,char node[4]);
void DeleteLast(nodetype *list);
functionality_functions.h
void print(nodetype *head);
nodetype *search(nodetype *list,char item[4]);
nodetype *create();
insert_functions.h
void *InsertLast(nodetype *list,char item[4]);
void *InsertAfter(nodetype *list,char item[4],char node[4]);
答案 0 :(得分:1)
根据GCC错误消息,delete_functions.c
文件中存在错误。
大概在开始时看起来像这样:
#include "delete_functions.h"
由于delete_functions.h
本身不包含types.h
,因此您需要先包含它:
#include "types.h"
#include "delete_functions.h"
或者,您可以在标题中添加 include防护,以便可以安全地多次包含它们,例如types.h
:
#ifndef TYPES_H
#define TYPES_H
typedef char AirportCode[4];
typedef struct nodetype{
char Airport[4];
struct nodetype *next;
} nodetype;
#endif
对于delete_functions.h
:
#ifndef DELETE_FUNCTIONS_H
#define DELETE_FUNCTIONS_H
void Delete(nodetype *list,char node[4]);
void DeleteLast(nodetype *list);
#endif
*_H
包含保护宏是必要的,因为否则,main.c
将不再编译:{{1}}中的每个类型只能为每个翻译单元定义一次,并且没有保护则每个types.h
将引入另一个定义,从而导致编译器错误。