【问题标题】:How to make struct type definitions visible outside a union C++如何使结构类型定义在联合 C++ 之外可见
【发布时间】:2011-10-14 00:35:46
【问题描述】:

我在 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;

}

我们可能(?)无法访问联合之外的结构 bk

【问题讨论】:

  • 对不起,这不是 C++。我们在这里不使用void*。或 C 风格的演员表。或者工会。或者比较不是运算符的函数。
  • @CatPlusPlus - 如果你能就此与我的老板会面,我会很高兴......
  • 有什么理由不能在联合之外定义/typedef bk 结构吗?

标签: c++ struct unions


【解决方案1】:

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

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;
    }
    

    现在,让我们来看看这不是 C++ 代码。首先,这些结构使用起来很麻烦——至少要添加构造函数。其次,联合并不是真正的类型安全——请改用boost::variant。第三,cmp_bk。让它operator==(const bk_type&amp;, const bk_type&amp;)——没有指针,没有void*,没有愚蠢的铸造。第四,固定大小的数组——几乎总是一个坏主意,会引发各种各样的问题。请改用std::vector。现在我的理智值已经用完了,所以我会完成的。

    【讨论】:

      猜你喜欢
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 2020-11-17
      • 2019-06-05
      • 2022-12-15
      • 2022-01-05
      相关资源
      最近更新 更多