2

メンバー変数ポインターを反復子として使用するメンバー関数を作成しています。ただし、読みやすさのために、関数内でポインターを参照したいと思います。そのようです:

/* getNext will return a pos object each time it is called for each node
 * in the tree. If all nodes have been returned it will return a Pos
 * object (-1, -1).
 * TODO: Add a lock boolean to tree structure and assert unlocked for
 *       push/pop.
 */
Pos BTree::getNext () const
{
    BTreeNode*& it = this->getNextIter;

    while (it)
    {
        if (it->visited)
        {
            /* node has been visited already, visit an unvisited right
             * child node, or move up the tree
             */
            if (   it->child [BTREE_RIGHT] != NULL
                && !it->child [BTREE_RIGHT]->visited)
            {
                it = it->child [BTREE_RIGHT];
            }
            else
            {
                it = it->parent;
            }
        }
        else
        {
            /* if unvisited nodes exist on the left branch, iterate
             * to the smallest (leftmost) of them.
             */
            if (   it->child [BTREE_LEFT] != NULL
                && !it->child [BTREE_LEFT]->visited)
            {
                for (;
                     it->child [BTREE_LEFT] != NULL;
                     it = it->child [BTREE_LEFT]) {}
            }
            else
            {
                it->visited = 1;
                return it->pos;
            }
        }
    }

    it = this->root;
    this->setTreeNotVisited (this->root);
    return Pos (-1, -1);
}

これは基本的に私が目指しているもので、this->getNextIter は BTreeNode* です。ただし、次のエラーが表示されます。

    btree.cpp:238: error: invalid initialization of reference of type
'DataTypes::BTreeNode*&' from expression of type 'DataTypes::BTreeNode* const'

この種の適切な構文は何ですか?

乾杯、

リス

4

1 に答える 1

3

メンバー関数はconst修飾されているため、メンバー変数を変更することはできませんgetNextIter。const 参照を使用する必要があります。

BTreeNode * const & it = getNextIter;

ただし、関数では を変更するため、代わりにメンバー関数から修飾子itを削除するか、メンバー変数を作成する必要があります。constgetNextItermutable

修飾されたメンバー関数があるconst場合、すべての非mutableメンバー変数はconstメンバー関数内で修飾されているため、コンパイラーは、 のgetNextIter内部で使用しようとするgetNext()と、型がDataTypes::BTreeNode* const( に注意してくださいconst) であると報告します。

于 2010-09-07T04:56:30.857 に答える