Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在multiple.c&;中使用struct typedef。h文件_C_Compiler Errors_Typedef - Fatal编程技术网

在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

目录包含以下文件:

  • “汽车”档案:
  • 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
    {
       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;