Search code examples
c++cportingunionsforward-declaration

Porting C -> C++, having trouble with accessing struct within unnamed union


I've been working on porting Marcel’s Simple Chess Program http://marcelk.net/mscp/ from C to C++. I have never worked much with unions, much less structs within unions. The top part I've listed is the declaration of the union. (All the code is in one .c file).

I've searched and read a few guides in addition to a C++ textbook, but I still have not gained any insight into how this could be fixed. I'm not sure what a "forward cdeclaration" is nor in what context I should be looking at the problem.

My compile options are

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;

These portions are examples of the lines giving errors:

error: forward declaration of ‘const struct cmp_bk(const void*, const void*)::bk’

error: invalid use of incomplete type ‘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;      

Solution

  • Looks like you've declared a class or struct bk somewhere earlier in your code. Also

    struct tt                       *tt;
    

    Will produce a error because you're trying to declares a variable with the same name as struct (both are called tt). Because of this error the variable is not properly declared and hence your other errors. In fact it looks like a lot of your problem's stem from naming data types (eg bk, tt) the same thing as a variable. Try changing the name of the data types or making them anonymous if you can.

    As a side note the structs inside the union could probably be made anonymous unless they're used anywhere else.