首页 文章

如何在联合C之外使结构类型定义可见

提问于
浏览
0

我在g下编译时遇到以下错误(这些是更长代码的段)


错误:无效使用不完整类型'const struct cmp_bk(const void *,const void *):: bk'错误:'const struct cmp_bk的前向声明(const void *,const void *):: bk'

守则如下:

static union {
    struct tt {                     /* Transposition table entry */
            unsigned short hash;    /* - Identifies position */
            short move;             /* - Best recorded move */
            short score;            /* - Score */
            char flag;              /* - How to interpret score */
            char depth;             /* - Remaining search depth */
    } tt[CORE];
    struct bk {                     /* Opening book entry */
            unsigned long hash;     /* - Identifies position */
            short move;             /* - Move for this position */
            unsigned short count;   /* - Frequency */
    } bk[CORE];
} core;

稍后在程序中,我们定义新的结构a,b

static int cmp_bk(const void *ap, const void *bp)
{
    const struct bk *a = (bk*) ap;
    const struct bk *b = (bk*) bp;

    if (a->hash < b->hash) return -1;
    if (a->hash > b->hash) return 1;
    return (int)a->move - (int)b->move;

}

我们可能(?)无法访问union之外的struct bk

2 回答

  • 2

    你可以在联合之外声明结构:

    struct tt {                     /* Transposition table entry */
            unsigned short hash;    /* - Identifies position */
            short move;             /* - Best recorded move */
            short score;            /* - Score */
            char flag;              /* - How to interpret score */
            char depth;             /* - Remaining search depth */
    };
    struct bk {                     /* Opening book entry */
            unsigned long hash;     /* - Identifies position */
            short move;             /* - Move for this position */
            unsigned short count;   /* - Frequency */
    };
    
    static union {
        struct tt tt[CORE];
        struct bk bk[CORE];
    } core;
    
  • 2

    这是将C代码编译为C的不良尝试 . 您无法在匿名类型中定义类型,并希望能够访问它 . 所以,修复后的代码是

    struct tt_type {                     /* Transposition table entry */
        unsigned short hash;    /* - Identifies position */
        short move;             /* - Best recorded move */
        short score;            /* - Score */
        char flag;              /* - How to interpret score */
        char depth;             /* - Remaining search depth */
    };
    
    struct bk_type {                     /* Opening book entry */
        unsigned long hash;     /* - Identifies position */
        short move;             /* - Move for this position */
        unsigned short count;   /* - Frequency */
    };
    
    static union {
        tt_type tt[CORE];
        bk_type bk[CORE];
    } core;
    
    static int cmp_bk(const void *ap, const void *bp)
    {
        const bk_type *a = (const bk_type*) ap;
        const bk_type *b = (const bk_type*) bp;
    
        if (a->hash < b->hash) return -1;
        if (a->hash > b->hash) return 1;
        return (int)a->move - (int)b->move;
    }
    

    现在,让's move onto how this is not C++ code. First of all, those structs are cumbersome to use — add constructors, at the very least. Second, the unions aren' t真的类型安全 - 改为使用 boost::variant . 第三, cmp_bk . 让它 operator==(const bk_type&, const bk_type&) - 没有指针,没有 void* ,没有愚蠢的铸造 . 第四,固定大小的数组 - 几乎总是一个坏主意,引发各种各样的问题 . 请改用 std::vector . 到现在为止我完成了.1728296_

相关问题