【问题标题】:"No match for 'operator=' in", structs error C++“不匹配'operator =' in”,结构错误C ++
【发布时间】:2011-12-08 01:53:52
【问题描述】:

当我在 g++ 下编译时,出现以下错误:

在函数'int search(int, int, int)'

1584:error: 在'* tt = & core.<anonymous union>::tt[((hash_stack[ply] >> 16) & 2047ul)]' 中与'operator=' 不匹配

1584:error: 注意:候选人是:

118:注:tt_type& tt_type::operator=(const tt_type&)

118:注意:没有已知的参数 1 从 'tt_type*''const tt_type&' 的转换

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_type                  *tt;                              //LINE 1584
    int                             oldalpha = alpha;
    int                             oldbeta = beta;
    int                             i, count=0;

    nodes++;

    /* test for draw by repetition */
    hash_stack[ply] = compute_hash();
    for (i=ply-4; i>=board[LAST]; i-=2) {
        if (hash_stack[i] == hash_stack[ply]) count++;
        if (count>=2) return 0;
    }

    /*
     *  check transposition table
     */
    *tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
    if (tt->hash == (hash_stack[ply] & 0xffffU)) {
        if (tt->depth >= depth) {
            if (tt->flag >= 0) alpha = MAX(alpha, tt->score);
            if (tt->flag <= 0) beta = MIN(beta,  tt->score);
            if (alpha >= beta) return tt->score;
        }
        best_move = tt->move & 07777;
    }

我之前定义的地方

struct tt_type {                                                       //LINE 118
    unsigned short hash;    /* - Identifies position */
    short move;             /* - Best recorded move */
    short score;            /* - Score */
    char flag;              /* - How to interpret score */
    char depth;             /* - Remaining search depth */
};

【问题讨论】:

  • 所以它的长短是:“没有已知的参数 1 从 'tt_type*' 到 'const tt_type&' 的转换”

标签: c++ struct operators


【解决方案1】:

错误信息中最重要的一行是:

118:note: no known conversion for argument 1 from 'tt_type*' to 'const tt_type&'

这实质上意味着您正在尝试为引用分配一个指针。

这反过来又让我认为将代码中的 * tt = &amp; core.::tt[((hash_stack[ply] &gt;&gt; 16) &amp; 2047ul)] 更改为 * tt = core.::tt[((hash_stack[ply] &gt;&gt; 16) &amp; 2047ul)] 用于深层复制或将 tt = &amp; core.::tt[((hash_stack[ply] &gt;&gt; 16) &amp; 2047ul)] 用于浅层复制将解决问题(取决于您的观点)。

【讨论】:

  • 没有浅拷贝和深拷贝。一个复制一个元素,另一个复制一个指针。
  • @BenVoigt:只要你明白了 :-) 它们都是不同事物的深层副本,哈哈
  • 复制结构可能是浅的或深的,这取决于operator= 的定义方式。
【解决方案2】:

我怀疑你的 1584 行真的是这一行:

*tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];

*tt 的类型为 struct tt_type。 RHS 的形式是&amp;...,所以它是某种指针类型。您可以将结构分配给结构,或将指针分配给指针,但不能将指针值分配给结构(除非您重载了赋值运算符)。

我没有研究足够的代码来理解它,但你可能想将*tt = ...更改为tt = ...

【讨论】:

    【解决方案3】:
    *tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
    

    您正在尝试将指针存储到不是指针的变量中。

    你需要一个

    *tt = TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
    

    复制数组的一个元素(这是行不通的,因为tt 没有初始化)

    tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
    

    创建一个指向数组的指针。

    第二个版本的另一种写法是

    tt = TTABLE + ((hash_stack[ply]>>16) & (CORE-1));
    

    【讨论】:

      【解决方案4】:

      在这一行:

      *tt = &TTABLE[ ((hash_stack[ply]>>16) & (CORE-1)) ];
      

      您正在尝试将tt_type 类型的变量分配给其他类型的另一个事物。我不知道TTABLE 是什么,但作为一个疯狂的猜测,尝试删除&amp;(如果TTABLEtt_types 的数组,&amp; 会导致错误。你会尝试将tt_type* 分配给tt_type)。

      【讨论】:

        【解决方案5】:

        *tt = &amp;TTABLE[/**/];

        您正在从指针分配您的结构。正如no known conversion for argument 1 from'tt_type*' to 'const tt_type&amp;' 所澄清的那样,它无法将tt_type* 转换为tt_type&amp; 以进行复制。

        我不知道TTABLE 是什么,但我会从中删除&amp;

        【讨论】:

          猜你喜欢
          • 2011-12-10
          • 2013-07-31
          • 1970-01-01
          • 2011-10-06
          • 1970-01-01
          • 2019-04-09
          • 2016-01-16
          • 1970-01-01
          • 2021-05-21
          相关资源
          最近更新 更多