redefinition of typedef

前端 未结 7 998
北荒
北荒 2020-12-15 05:54

I am possibly doing this incorrectly and this is much a question about why it works in one compiler and not the other.

I have a large C application, and I am

7条回答
  •  误落风尘
    2020-12-15 06:11

    One piece of the idiom is missing. The forward declarations are independent from the definitions, so they should be in a separate header file.

    // a_fwd.h
    
    #ifndef A_FWD_H
    #define A_FWD_H
    
    typedef struct A_ A;
    
    #endif
    
    // a.h
    
    #ifndef A_H
    #define A_H
    
    #include "a_fwd.h"
    
    struct A_ {
    };
    
    #endif
    

    Now it's always safe to include any headers in any order.


    It is illegal to have two definitions of anything. A typedef is a definition, not just a declaration, so the one compiler was being quite lax to allow the redundancy.

提交回复
热议问题