我正在创建一个类来生成模板化的Median堆。我有两个类:一个Median Heap(包含一个maxHeap和一个minHeap对象)和一个Heap对象。 我试图让MedianHeap类成为Heap类的朋友,因为我需要访问数据成员" maxHeap"和" minHeap"来自Heap类。 这些是我遇到的主要错误,我花了好几个小时试图找出原因:
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之上的模板语法,但是代码仍然产生了一堆私有&#39;错误。以下是一些错误,其中一个错误出现在:
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 :(得分:0)
在Heap
的定义开始之前添加T
的前向声明,然后在Heap
内添加一个引用template <typename T>
class MedianHeap;
template <typename T>
class Heap
{
friend class MedianHeap<T>;
public:
// ...
};
实例化的好友声明:
minHeap
对于maxHeap
和Heap
,如果我理解正确,他们不需要被模仿,他们可以只使用封闭T
的即时消息,因为他们肯定需要相同的template <typename T>
class MedianHeap {
// ...
Heap<T> minHeap;
Heap<T> maxHeap;
};
,不要他们吗?
n