Compiler error with struct definition in header file

P. *_*ich 2 c struct header-files

I keep getting a compiler issue when trying to use a struct I defined in a header file.

我有两个文件main.c

     #include <stdio.h>
     #include <stdlib.h>
     #include "node.h"

     int main(){
         struct NODE node;
         node.data = 5;
         printf("%d\n", node.data);
         return 0;
     }
Run Code Online (Sandbox Code Playgroud)

以及node.h

#ifndef NODE
#define NODE
    struct NODE{
        int data;
        struct NODE *next;
    };

#endif
Run Code Online (Sandbox Code Playgroud)

我正在写一个小程序来用C进行一些模块化编程,但是却遇到了以下编译器错误:

node.h:5:21: error: expected ‘{’ before ‘*’ token
         struct NODE *next;
                     ^
Run Code Online (Sandbox Code Playgroud)

我得到了main.c编译,做我想它的时候,我定义做了什么struct,直接在文件中main.c,但由于某种原因,它不会工作,如果我把一个头文件中的定义,然后尝试把它列入main.c。这非常令人沮丧,我敢肯定这是一件小事,但是有人可以告诉我为什么这不起作用吗?从我一直在阅读的书中,我应该能够做到这一点,不是吗?

非常感谢!

Pie*_*ths 6

预处理器没有扩展NODE,因为您已经使用宏定义了它。将头文件更改为如下所示:

#ifndef NODE_H
#define NODE_H
    struct NODE{
        int data;
        struct NODE *next;
    };

#endif
Run Code Online (Sandbox Code Playgroud)