C++ friend 关键字不访问非静态数据成员

C++ friend keyword not accessing non-static data member

我需要使用新功能重载 ostream 运算符以实现双向链接的跳过列表 class。

当我计算 class 的实例时,我希望它遍历我的跳过列表的级别,以及 head 指针指向 nullptr 的任何位置我希望它打印关卡名称和空状态。

看起来像:

After adding 7
Level: 4 -- empty
Level: 3 -- empty
Level: 2 -- empty
Level: 1 -- empty
Level: 0 -- 7

我需要动态输入关卡数。我尝试分配 int level = SkipList::maxLevels_; 但出现错误 invalid use of non-static data member

我已将 ostream 设为 friend。我如何指示它访问 maxLevels_ 数据成员?

SkipList.h

#include <stdio.h>
#include <iostream>

#ifndef SKIP_LIST_
#define SKIP_LIST_

using namespace std;
class SkipList
{
   private:

      struct SkipListNode {
         // Convenience constructor to create node, set its data, and set all pointers to nullptr
         explicit SkipListNode(int data){
            data_ = data;
            next_ = NULL;
            prev_ = NULL;
            upLevel_ = NULL;
            downLevel_ = NULL;
         }

         // data for SNode
         int data_;
          // link to next at same level
         SkipListNode* next_;
         // link to previous at same level
         SkipListNode* prev_;
         // link up one level
         SkipListNode* upLevel_;
         // link down one level
         SkipListNode* downLevel_;
      };

      // maximum # of levels of SkipList, levels are 0 to maxLevels-1
      int maxLevels_;
      // array of maxLevels_ SkipListNode pointers as head pointers. For example,
      // if maxLevels_ == 2, we'd have Heads[0] and Heads[1]. Dynamically allocated
      // by constructor.
      SkipListNode** heads_;
      // array of maxLevels_ SkipListNode pointers as tail pointers.
      SkipListNode** tails_;
      // given a pointer to a SkipListNode, place it before the given nextNode
      void addBefore(SkipListNode* newNode, SkipListNode* nextNode, int level);
      // return true 50% of time,
      // each node has a 50% chance of being at higher level
      bool alsoHigher() const;
      
   public:
      //Constructor
      SkipList(){maxLevels_ = 1;}
      SkipList(int maxLevels);
   
      //Destructor
     // virtual ~SkipList();
      // return true if successfully added, no duplicates
      bool insert(int item);
      // item deletion; return true if successfully removed
      bool erase(int item);
      // return true if found in SkipList
      bool contains(int item) const;

      friend ostream& operator<<(ostream& os, const SkipList& list){
         int level = SkipList::maxLevels_;
         while (level >= 0) {
            SkipListNode* temp = list.heads_[level];
            if (temp == nullptr) {
               os << "Level: " << level << "-- empty";
               
            }
            else {
               while (temp) {
                  os << temp->data_ << " ";
                  temp = temp->next_;
               } 
            }
            os << endl;
            level--;
         }
      }
};

#endif

SkipList::maxLevels_; 指的是 SkipList class 的静态 maxLevels_ 成员。 因此,如果您需要 maxLevels_SkipList 所有实例的最大级别,则必须将其声明为 static。 否则在你重载的朋友函数中你必须使用 list 实例的私有成员。

friend ostream& operator<<(ostream& os, const SkipList& list){
         int level = list.maxLevels_;
...