2017-11-18 7 views
2

私はテンプレートのメディアヒープを生成するためにクラスで作業しています。私は2つのクラスを持っています:中央ヒープ(maxHeapとminHeapオブジェクトを保持します)とヒープオブジェクト。 Heapクラスのメンバー "maxHeap"と "minHeap"にアクセスする必要があるため、MedianHeapクラスをHeapクラスのフレンドにしようとしています。 これらは私が取得していますし、私は理由を把握しようと時間を費やしてきた主なエラーです:フレンドクラスが動作しませんC++、 'クラスT'宣言のテンプレートパラメータ

MedianHeap.h:61: error: declaration of ‘class T’ 
MedianHeap.h:52: error: shadows template parm ‘class T’ 
MedianHeap.h:62: error: ‘MedianHeap’ is not a template 
MedianHeap.h:115: error: declaration of ‘class T’ 
MedianHeap.h:150: error: ‘minHeap’ was not declared in this scope 
MedianHeap.h:151: error: ‘maxHeap’ was not declared in this scope 


template <typename T> 
class Heap 
{ 

public: // after adding in MedianHeap<T>:: scope operator, changing 
    // Heap class contents from private: to public: got rid of all of 
    // the 'is private' errors that occurred when I did g++ MedianHeap.h 1.cpp 
    // and finally, 
    // got rid of all the 
    template <typename T> 
    friend class MedianHeap<T>; 


    Heap(int cap); 
    int numItems(); 
    void add(T newItem); 
    void fixMinHeap(); 
    void fixMaxHeap(); 
    int parent(int index); 
    int rightChild(int index); 
    int leftChild(int index); 
    void minHeapify(int root); 
    void maxHeapify(int root); 
    T removeMin(); 
    T removeMax(); 
    void swap(int a, int b); 
    bool searchAndRemove(T& givenItem, bool (*equalTo) (const T&, const T&)); 
    T locate(int pos); 
    int num; 
    int capacity; 
    bool (*compare) (const T&, const T&); 
    T heap[]; //////////// needed, right ? //////////////// 
}; 


template <typename T> 
class MedianHeap { 

public: 
    //friend class Heap<T>; 
    MedianHeap(bool (*lt) (const T&, const T&), bool (*gt) (const T&, const T&), int cap=100); 
    void insert(const T& item); 
    T getMin(); 
    T getMax(); 
    T getMedian(); 
    int size(); 
    int capacity(); 
    bool deleteItem(T& givenItem, bool (*equalTo) (const T&, const T&)); 
    void dump(); 
    int maxHeapSize(); 
    int minHeapSize(); 
    T locateInMaxHeap(int pos); 
    T locateInMinHeap(int pos); 
private: 
    void fixImbalance(); 
    bool isEmpty(); 
void searchForNewMax(); 
    void searchForNewMin(); 
    void searchForNewMedian(); 
    // stores all the numbers less than the current median in a maxHeap.i.e median is the maximum, at the root              \ 

    template <typename T> 
    Heap<T> minHeap; 
    //stores all the numbers greater than the current median in a minheap, i.e median is the minimum, at the root              \ 

    template <typename T> 
    Heap<T> maxHeap; 
    // tracks total number of items in the whole data structure 
    T max; 
    T min; 
    T median; 
    int total; 
    int cap; 
}; 

は、だから私は前方宣言を追加してのminheapとmaxHeap上のテンプレート構文を削除しますが、コードはまだA生成「プライベート」エラーの束。ここまで来て、エラーとそのエラーが表示されるに機能の一つのいくつかは、次のとおりです。

MedianHeap.h: In member function ‘void Heap<T>::fixMinHeap()’: 
MedianHeap.h:526: error: ‘minHeap’ was not declared in this scope 

///////////////////// 
//     // 
// FIX MIN HEAP // 
//     // 
///////////////////// 
template <typename T> 
void Heap<T>::fixMinHeap(){ 
    // get this entry to the right place 
    // start at where the item was added, go all the way up to the root if need be 
    int i = num; 
    while (i != 0 && minHeap.compare(heap[i],heap[parent(i)])){ 
     swap(i, parent(i)); 
     i = parent(i); // O(logn) 
    } 
    //// comparator needs to be used here //////^^^^^^^^ 
} 
+0

プラス1あなたがそれに値する。ベスト。 – statosdotcom

答えて

0

Heapの定義が始まる前に、その後Tでインスタンス化を参照friend宣言を追加し、MedianHeapの前方宣言を追加します。 Heapの内側:

minHeapmaxHeapについては
template <typename T> 
class MedianHeap; 

template <typename T> 
class Heap 
{ 
    friend class MedianHeap<T>; 

public: 
    // ... 
}; 

、私が正しく理解していれば、彼らは、彼らはちょうど囲むHeapのinstantationsを使用することができますtemplatisedする必要はありません、なぜなら彼らは確かに同じを必要としますT、そうではありませんか?

template <typename T> 
class MedianHeap { 

// ... 
    Heap<T> minHeap; 
    Heap<T> maxHeap; 
}; 
+0

ありがとうございました..私はあなたの修正を加えましたが、コードはまだ '私的なものです'というエラーを生成します。エラーを生成する関数の1つとともに印刷される新しいエラーを含めました。友人の構文がこれらの「プライベート」エラーを取り除かなかった理由を教えてください。 – Coder

関連する問題