我的代码在下面,我在numberCollisions + = crashNum遇到错误;
错误状态'numberCollisions'无法修改,因为它正在通过const对象使用。因为它是一个const函数,会弹出这个错误吗?谢谢您的帮助。

int HashTable_qp::findPos( const string & x ) const
{
    /* 1*/      int collisionNum = 0;
    /* 2*/      int currentPos = myhash( x, array.size( ) );

    /* 3*/      while( array[ currentPos ].info != EMPTY &&
        array[ currentPos ].element != x )
    {
        /* 4*/          currentPos += 2 * ++collisionNum - 1;  // Compute ith probe
        /* 5*/          if( currentPos >= array.size( ) )
            /* 6*/              currentPos -= array.size( );
    }
    numberCollisions += collisionNum;
    /* 7*/      return currentPos;
}

最佳答案

是的,正是因为这个原因。常量方法意味着您不会更改实例,但是您会这样做,因为findPos应该会更改实例。因此,只需删除const并继续。

09-29 21:31