字段类型不完整;结构已经声明

Field has incomplete type; Structure already declared

我目前正在用 C 编程,并且有

typedef struct Stack Stack;

struct Stack {
    Stack above;
    Tree *t;
    char *tag;
};

我遇到错误 "field 'above' has incomplete type" 我已经在其上方的 typedef 中声明了该结构,所以我不确定为什么会遇到问题。我环顾四周,但没有发现任何与此相关的东西。谢谢!

你可以在你的结构中有一个指向你的结构的指针,但不是一个实际的结构,对于堆栈,这可能是你所追求的。

typedef struct Stack Stack;

struct Stack {
    Stack *above;
    Tree *t;
    char *tag;
};

想想看,如果你在结构 A 中有一个结构 B,那么在结构 B 中会有一个结构 C,在结构 C 中会有一个结构 D...继续。

struct Stack {
    Stack above {
        Stack above {
            Stack above {
                Stack above
                         ...............
                Tree *t;
                char *tag;
            };
            Tree *t;
            char *tag;
        };
        Tree *t;
        char *tag;
    };
    Tree *t;
    char *tag;
};