开发者

How do I fix "invalid type argument of 'unary*'?

开发者 https://www.devze.com 2023-04-13 03:54 出处:网络
Okay, so i\'m reposting this but also including an update, Probably against the rules, but I couldn\'t find any other way to do so. Someone asked for a description on my class and I couldn\'t figure o

Okay, so i'm reposting this but also including an update, Probably against the rules, but I couldn't find any other way to do so. Someone asked for a description on my class and I couldn't figure out how to add it, so if someone can help me figure out how to edit my post that would also be greatly appreciated. So I've been running into an error "Invalid type argument of 'unary*' while using this code

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

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

The exact error I get is:

invalid type argument of 'unary *'

and it's set to both the lines

dynamic_cast<const struct bk>(*a)=ap;
dynamic_cast<const struct bk>(*b)=bp;

so someone told me to rewrite it using a reinterpret_cast instead of a dynamic, but kept getting "undelcared" variables, so I just starting making things up to get something to work so I know it's wrong and this is my current result with the new errors开发者_Go百科 "expected primary-expression before '->' token" for all the '->' tokens.

static int cmp_bk( const void *ap, const void *bp)
{
   class bk {};
   class pa {};
   class pb {};
   class a {};
   class b {};
   bk * pa;
   bk * pb;
    ap = reinterpret_cast<bk*>(pa);
    bp = reinterpret_cast<bk*>(pb);

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

also my bk class is within a static union so here is all of it.

 /* Transposition table and opening book share the same memory */
 static union {
 #define TTABLE (core.tt) 
    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];
 #define BOOK (core.bk)       
    struct bk {                     /* Opening book entry */
            unsigned long hash;     /* - Identifies position */
            short move;             /* - Move for this position */
            unsigned short count;   /* - Frequency */
    } bk[CORE];
} core;

//#define TTABLE (core.tt)
//#define BOOK (core.bk)

hopefully this is all the information people need, as I said if someone can also help me to edit my post so I don't have to make a new one that would be greatly appreciated. and thanks for everyone's help that helped on the last post and come to help on this new post.


In this line:

dynamic_cast<const struct bk>(*a)=ap;

You are attempting to dereference an int. Only pointers and classes with overloaded operator* can be dererenced.


To answer your question directly:

const bk& a_bk = *reinterpret_cast<const bk*>(ap);
const bk& b_bk = *reinterpret_cast<const bk*>(bp);

Second, the code:

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

Can either be reduced to:

if ( a_bk.hash < b_bk.hash ) return -1;
else if (a_bk.hash > b_bk.hash ) return 1;
else return 0;

Or just:

return a_bk.hash - b_bk.hash;

Third, use a type-safe sort function over use an unsafe C-style sort function:

int sort_bk_by_hash( const bk& lhs, const bk& rhs ) {
    return rhs.hash - lhs.hash;
}

// then use like so:
set<bk,sort_bk_by_hash> data;

// or:
vector<bk> data;
sort( data.begin(), data.end(), sort_bk_by_hash );

Fourth, how do you know which structure inside the union is activated without some sort of type field? Can the type be inferred from the hash value?

Lastly, you may be able to accomplish whatever it is you're trying to do with boost::variant.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号