代码之家  ›  专栏  ›  技术社区  ›  Steve

forward声明typedef的结构

  •  4
  • Steve  · 技术社区  · 15 年前

    我不知道如何转发声明Windows结构。定义是

    typedef struct _CONTEXT
    {
     ....
    } CONTEXT, *PCONTEXT
    

    我真的不想进入这个标题,因为它被包括在任何地方。

    我试过了

    结构上下文

    结构上下文

    没有运气(用winnt.h中的actuall结构重新定义基本类型)。

    2 回复  |  直到 13 年前
        1
  •  9
  •   MSN    15 年前
    extern "C" { typedef struct _CONTEXT CONTEXT, *PCONTEXT; }
    

    你需要声明 _CONTEXT 是一个 struct . 宣布为 extern "C" 匹配windows.h的外部链接(这是一个c头文件)。

    但是,您不需要为 typedef ,但如果这样做,则所有定义都必须匹配 One Definition Rule )

    编辑:我也忘记了外部的“C”。

        2
  •  1
  •   Alexey Malistov    15 年前

    不是解决方案,而是解决方案:

    // h-file
    struct MyContext; // forward decl
    void f(MyContext * pContext); // use pointer
    
    
    //cpp-file
    #include <windows.h>
    struct MyContext {
       CONTEXT cont;
    };
    
    void f(MyContext * pContext)
    {
       CONTEXT * p_win_cont = & pContext->cont;
       // use p_win_cont
       // ....
    }