0

こんにちはこれはコードです:

template <class T> class FibonacciHeap{
public:
    class Entry{
        public:
            // Returns the element represented by this heap entry.
            T getValue(){
                return mElem;
            }

            // Sets the element associated with this heap entry.
            void setValue(T value){
                    mElem = value;
            }

            // Returns the priority of this element.
            double getPriority(){
                return mPriority;
            }

        private:
            int mDegree = 0;                // Number of children
            bool mIsMarked = false;         // Whether the node is marked

            Entry mNext;                    // Next element in the list
            Entry mPrev;                    // Previous element in the list

            Entry mChild;                   // Child node, if any
            Entry mParent;                  // Parent node, if any
            T mElem;                        // Element being stored here
            double mPriority;               // Its priority

            //Constructs a new Entry that holds the given element with the indicated priority.
            Entry(T elem, double priority){
                mNext = mPrev = this;
                mElem = elem;
                mPriority = priority;
            }
    };
    ...

クラス「エントリ」では、エントリを再帰的に呼び出したいので、次を使用できます。

    First_entry.mPrev.mNext

これが Java で機能することはわかっていますが、これを C++ でコンパイルすると、次のようになります。

    error: 'FibonacciHeap<T>::Entry::mNext' has incomplete type

これを修正する方法または回避する方法を知っている人はいますか?

4

1 に答える 1

3

ここにある変数名と初期化子に基づいて、私のJava Fibonacci ヒープを C++ に適応させようとしていると思います。:-) もしそうなら、頑張ってください!

Java では、 type の変数がある場合、それは正直なオブジェクトではなく別のオブジェクトへのポインターであるという点でEntry、 type の C++ 変数のように機能します。その結果、クラスの定義では、フィールドが .ではなくタイプになるようにフィールドを調整する必要があります。同様に、演算子を使用してフィールドを選択する代わりに、演算子を使用する必要があります。そうEntry*EntryEntryEntryEntry*Entry.->

First_entry.mPrev.mNext

のように書き換えられます

First_entry->mPrev->mNext

Entryへのポインターを明示的に初期化することを忘れないでくださいnullptr。Java はこれを自動的に行います。そのため、Java バージョンには初期化子がありません。ただし、C++ は初期化されていないポインタにガベージ値を与えるため、明示的な値を与えるようにしmChildmParentくださいnullptr

于 2017-11-07T01:00:03.883 に答える