在multiple.c&;中使用struct typedef。h文件
目录包含以下文件:在multiple.c&;中使用struct typedef。h文件,c,compiler-errors,typedef,C,Compiler Errors,Typedef,目录包含以下文件: “汽车”档案: a。car.h: #ifndef __CAR_H__ #define __CAR_H__ typedef struct car car_t; ... (some functions declarations) ... #endif /* __CAR_H__ */ b。汽车 #include <stdio.h> #include <stdlib.h> #include "car.h" typedef struct car_node
#ifndef __CAR_H__
#define __CAR_H__
typedef struct car car_t;
...
(some functions declarations)
...
#endif /* __CAR_H__ */
b。汽车
#include <stdio.h>
#include <stdlib.h>
#include "car.h"
typedef struct car_node
{
void *data;
struct car_node *next;
struct car_node *prev;
} car_node_t;
struct car
{
car_node_t head;
car_node_t tail;
};
...
(some functions implementations)
...
b。车辆
#include <stdio.h>
#include "car.h"
#include "vehicles.h"
struct vehicles
{
car_t carlist;
void *data;
};
我的问题是:如果car.h包含在vehicles.c中,为什么编译器不识别car.h中的typedef car\u t?问题是在
vehicles.c中,编译器需要知道什么是car\u t
,并且您只提供了它应该被调用的内容。实际上什么是car\u t
在car.c
中定义。要解决此问题,您必须将carlist
设置为指针(因为编译器不需要完整的类型),或者必须将结构定义移动到.h
文件:
car.h
:
typedef struct car_node
{
void *data;
struct car_node *next;
struct car_node *prev;
} car_node_t;
typedef struct car {
car_node_t head;
car_node_t tail;
} car_t;
#include <stdio.h>
#include "car.h"
#include "vehicles.h"
struct vehicles
{
car_t carlist;
void *data;
};
#include <stdio.h>
#include "car.h"
#include "vehicles.h"
int main()
{
...
(some tests)
...
}
vehicles.c: error: field ‘carlist’ has incomplete type
typedef struct car_node
{
void *data;
struct car_node *next;
struct car_node *prev;
} car_node_t;
typedef struct car {
car_node_t head;
car_node_t tail;
} car_t;