How to make struct type definitions visible outside a union C++
I keep getting the following errors when I compile under g++ (These are segments of a much longer code)
error: invalid use of incomplete type 'const struct cmp_bk(const void*, const void*)::bk'
error: forward declaration of 'const struct cmp_bk(const void*, const void*)::bk'
The Code is as follows:
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 sc开发者_如何学运维ore */
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;
Later on in the program, we define new structs 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;
}
We're possibly(?) having trouble accessing the struct bk outside of the union
You could just declare the structs outside of the 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 */
};
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;
This is a poor attempt at compiling C code as C++. You cannot define a type inside an anonymous type and expect to be able to access it. So, the code after fixing this is
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;
}
Now, let'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 really type-safe — use boost::variant
instead. Third, cmp_bk
. Make it operator==(const bk_type&, const bk_type&)
— no pointers, no void*
, no silly casting. Fourth, fixed-size arrays — almost always a bad idea, inviting all sorts of problems. Use std::vector
instead. And by now I'm running out of my sanity points, so I'll finish.
精彩评论