0

さて、ネストされたクラスLinkedListIteratorを持つクラスLinkedListがあります。LinkedListIteratorのメソッド内で、LinkedListのプライベートフィールドを参照します。私はそれが合法だと思った。しかし、エラーが発生します:

from this location

私がそれらを参照するたびに。

そして、囲んでいるクラスのフィールドに対応するエラーメッセージが表示されます。

invalid use of non-static data member 'LinkedList<int>::tail'

なぜですか?関連するコードは次のとおりです。

template<class T>
class LinkedList {

    private:
        //Data Fields-----------------//
        /*
         * The head of the list, sentinel node, empty.
         */
        Node<T>* head;
        /*
         * The tail end of the list, sentinel node, empty.
         */
        Node<T>* tail;
        /*
         * Number of elements in the LinkedList.
         */
        int size;

    class LinkedListIterator: public Iterator<T> {

            bool add(T element) {

                //If the iterator is not pointing at the tail node.
                if(current != tail) {

                    Node<T>* newNode = new Node<T>(element);
                    current->join(newNode->join(current->split()));

                    //Move current to the newly inserted node so that
                        //on the next call to next() the node after the
                        //newly inserted one becomes the current target of
                        //the iterator.
                    current = current->next;

                    size++;

                    return true;
                }

                return false;
            }
4

1 に答える 1

2

staticそのような非メンバーだけを使用することはできません。次の例で問題が解決すると思います。

LinkedList<int>::LinkedListIterator it;
it.add(1);

メソッドの内部には何がcurrentありますか?tail話すインスタンスがないLinkedListので、それらのメンバーはまだ存在しません。

メンバーを作ると言っているわけではありませんがstatic、それは間違っていますが、アプローチを再考してください。

stdイテレータがどのようになっているのか調べてください。

于 2012-08-23T06:13:12.100 に答える