c - 嵌套Struct的重新定义

标签 c struct declaration

我正在使用 asn1c 转换后的代码,它采用了我的 ASN.1 代码并将其转换为 C,这样,就有了一整套复杂的类型和结构已声明。目前,我收到此错误:

BACnetFaultParameter.h:125:11: error: redefinition of ‘struct list_of_fault_values’ BACnetFaultParameter.h:78:11: note: originally defined here

我当前的代码如下所示(请注意,这些代码块是包含在同名 .c 文件中的 .h 文件):

 typedef struct BACnetFaultParameter {
    BACnetFaultParameter_PR present;
    union BACnetFaultParameter_u {
            NULL_t   none;
            struct fault_characterstring {

                    struct list_of_fault_values {
                            A_SEQUENCE_OF(CharacterString_t) list;

                            /* Context for parsing across buffer boundaries */
                            asn_struct_ctx_t _asn_ctx;
                    } list_of_fault_values;

                    /* Context for parsing across buffer boundaries */
                    asn_struct_ctx_t _asn_ctx;
            } fault_characterstring;

然后在代码中的几行中,我被告知要重新声明struct list_of_fault_values

                 struct fault_state {

                    struct list_of_fault_values {
                            A_SEQUENCE_OF(struct BACnetPropertyStates) list;

                            /* Context for parsing across buffer boundaries */
                            asn_struct_ctx_t _asn_ctx;
                    } list_of_fault_values;
                  /* Context for parsing across buffer boundaries */
                    asn_struct_ctx_t _asn_ctx;
            } fault_state;

令我困惑的是,我在这两个代码块周围使用了 #ifndef#define 语句,然后我收到错误消息

"Fault state has no member named list_of_fault_values"

.c 文件中。 list_of_fault_values 是本地结构,但它的行为就像全局结构,运行 grep 也没有给我指出任何可能的问题。我的问题是。有人有想法吗?或者有人遇到过类似的事情吗?

这是带有#ifndef语句的代码

                   struct fault_characterstring {
    #ifndef _LIST_OF_FAULT_VALUES
    #define _LIST_OF_FAULT_VALUES
            struct list_of_fault_values {
                            A_SEQUENCE_OF(CharacterString_t) list;

                            /* Context for parsing across buffer boundaries */
                            asn_struct_ctx_t _asn_ctx;
                    } list_of_fault_values;
                    #endif
             /* Context for parsing across buffer boundaries */
                    asn_struct_ctx_t _asn_ctx;
            } fault_characterstring;

      #ifndef _LIST_OF_FAULT_PARAMETERS
    #define _LIST_OF_FAULT_PARAMETERS
                    struct list_of_fault_values {
                            A_SEQUENCE_OF(struct BACnetPropertyStates) list;

                            /* Context for parsing across buffer boundaries */
                            asn_struct_ctx_t _asn_ctx;
                    } list_of_fault_values;
                    #endif
                     /* Context for parsing across buffer boundaries */
                    asn_struct_ctx_t _asn_ctx;
            } fault_state;

最佳答案

使用 #ifndef 的想法是正确的,但仍然需要声明 list_of_fault_values,即使该结构已经声明。

为了简单起见,您可以在 #endif 之前添加一个 #else block :

#ifndef HAVE_LIST_OF_FAULT_PARAMETERS // NB: you shouldn't use a leading underscore for macros
#define HAVE_LIST_OF_FAULT_PARAMETERS
    struct list_of_fault_values {
        ...
    } list_of_fault_values;
#else // just use the previous definition
    struct list_of_fault values list_of_fault_values;
#endif

关于c - 嵌套Struct的重新定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25316169/

相关文章:

c - 在c中访问数组的-1元素

javascript - $(document.body) 和 document.body 是一样的吗?上课打扫垃圾和捆绑? - MooTools 1.3

c++ - 在 C++ 中声明和初始化变量的最佳实践

c - 任何数据结构的大小都可以被 int 的大小整除吗?

c - 使用全局变量作为函数的参数

c - 为什么编译器会发出 "warning: assignment makes integer from pointer without a cast"?

c - 如果函数写在主函数之上,则不需要函数声明。为什么?

c - 如何正确退出子进程?

linux - linux 中 whereis 命令的 API

c - 获取 C 中结构体的大小