“必须使用'struct'标记来引用类型'node'”。 [英] "Must use 'struct' tag to refer to type 'node'"

查看:521
本文介绍了“必须使用'struct'标记来引用类型'node'”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是怎么回事?我正在


必须使用'struct'标记来引用类型'node'

Must use 'struct' tag to refer to type 'node'

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side    
} node;

我也尝试过

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
};

struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
};

typedef struct  {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
} node;

typedef struct treeNode {
    char * fx; // function
    treeNode  * gx; // left-hand side
    char * op; // operator
    treeNode * hx; // right-hand side
} node;

我都为这些错误。再次在普通C语言中正确的语法是什么?

and I get errors for all of those. What is the proper syntax in plain C again?

推荐答案

让我们看一下第一个代码段:

Let's look at the first snippet:

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side    
} node;

gx hx 出现在结构节点 / 节点类型定义的中间,在 typedef 语句已完成。在程序中,此时 node 不是有效的类型名称,因为 typedef 尚未结束(并且与C ++不同,编写 struct节点{...}; 不会自动将 node 设为类型名称)。但是,此时 struct节点 是有效的类型名称(只要您仅将其用于指针类型),因此为了声明 gx hx 正确,您需要编写:

gx and hx occur in the middle of the struct node/node type definition, before the typedef statement is complete. At this point in the program, node is not a valid type name, because the typedef isn't over yet (and, unlike C++, writing struct node { ... }; does not automatically make node a type name). However, struct node is a valid type name at this point (as long as you only use it for pointer types), so in order to declare gx and hx properly, you need to write:

typedef struct node {
           char * fx; // function
    struct node * gx; // left-hand side
           char * op; // operator
    struct node * hx; // right-hand side    
} node;

这篇关于“必须使用'struct'标记来引用类型'node'”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆