【问题标题】:Porting C -> C++, having trouble with accessing struct within unnamed union移植 C -> C++,在访问未命名联合中的结构时遇到问题
【发布时间】:2013-01-28 06:06:50
【问题描述】:

我一直致力于移植 Marcel 的简单国际象棋程序 http://marcelk.net/mscp/ 从 C 到 C++。我从来没有与工会合作过,更不用说工会内的结构了。 我列出的最上面的部分是工会的声明。 (所有代码都在一个 .c 文件中)。

除了 C++ 教科书外,我还搜索并阅读了一些指南,但我仍然没有深入了解如何解决此问题。我不确定什么是“前向 cdeclaration” 也不是我应该在什么情况下看待这个问题。

我的编译选项是

g++ -ansi -Wall -O2 -pedantic -o mscp -mscp.cpp

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;

这些部分是给出错误的行的示例:

错误:'const struct cmp_bk(const void*, const void*)::bk' 的前向声明

错误:不完整类型“const struct cmp_bk(const void*, const void*)::bk”的使用无效

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

            if (a->hash < b->hash) return -1; //ERROR HERE
            if (a->hash > b->hash) return 1; //ERROR HERE
            return (int)a->move - (int)b->move; //ERROR HERE
    }

static int search(int depth, int alpha, int beta)
{
        int                             best_score = -INF;
        int                             best_move = 0;
        int                             score;
        struct move                     *moves;
        int                             incheck = 0;
        struct tt                       *tt; //ERROR HERE
        int                             oldalpha = alpha;
        int                             oldbeta = beta;
        int                             i, count=0;

               if (tt->depth >= depth) {
                    if (tt->flag >= 0) alpha = MAX(alpha, tt->score); //ERROR HERE
                    if (tt->flag <= 0) beta = MIN(beta,  tt->score); //ERROR HERE
                    if (alpha >= beta) return tt->score;
            }
            best_move = tt->move & 07777;      

【问题讨论】:

  • 只是谷歌前向声明中出现的众多链接之一。请参阅此 SO question 的已接受答案以获得解释,或者您可以通过搜索找到的任何其他答案。

标签: c++ c porting unions forward-declaration


【解决方案1】:

看起来您已经在代码的前面某处声明了classstruct bk。还有

struct tt                       *tt;

会产生错误,因为您试图声明一个与 struct 同名的变量(两者都称为tt)。由于此错误,变量未正确声明,因此您的其他错误。事实上,看起来你的很多问题都源于将数据类型(例如bktt)命名为与变量相同的东西。如果可以,请尝试更改数据类型的名称或使其匿名。

作为旁注,除非它们在其他任何地方使用,否则联合内部的结构可能会匿名。

【讨论】:

    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 2015-11-04
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2012-09-16
    • 1970-01-01
    相关资源
    最近更新 更多