【问题标题】:Friend class not working C++ , declaration of 'class T' shadows template parameter朋友类不工作 C++,'class T' 阴影模板参数的声明
【发布时间】:2018-05-02 07:52:02
【问题描述】:

我正在研究一个生成模板中值堆的类。我有两个类:一个中值堆(包含一个 maxHeap 和一个 minHeap 对象)和一个 Heap 对象。 我试图让 MedianHeap 类成为 Heap 类的朋友,因为我需要从 Heap 类访问数据成员“maxHeap”和“minHeap”。 这些是我遇到的主要错误,我花了几个小时试图找出原因:

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 上面的模板语法,但代码仍然会产生一堆“is private”错误。以下是出现的一些错误以及出现该错误的函数之一:

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 //////^^^^^^^^
}

【问题讨论】:

    标签: c++ scope


    【解决方案1】:

    Heap 的定义开始之前添加MedianHeap 的前向声明,然后在Heap 内部添加一个引用实例化的友元声明,其中T

    template <typename T>
    class MedianHeap;
    
    template <typename T>
    class Heap
    {
        friend class MedianHeap<T>;
    
    public:
        // ...
    };
    

    至于minHeapmaxHeap,如果我理解正确的话,它们不需要模板化,它们可以使用封闭Heap的实例化,因为它们当然需要相同的T,不要不是吗?

    template <typename T>
    class MedianHeap {
    
     // ...
        Heap<T> minHeap;
        Heap<T> maxHeap;
    };
    

    【讨论】:

    • 谢谢你。我添加了你的更正,但代码仍然产生“是私人的”错误。我包括打印的新错误以及产生错误的函数之一。您能否告诉我为什么朋友语法没有消除这些“是私有的”错误?
    猜你喜欢
    • 2020-01-28
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 2014-10-03
    • 2013-09-28
    • 2014-07-23
    相关资源
    最近更新 更多