首页 > 解决方案 > C++朋友关键字不访问非静态数据成员

问题描述

我需要ostream使用双重链接的跳过列表类的新功能来重载运算符。

当我计算我的类的实例时,我希望它遍历我的跳过列表的级别,并且无论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

我已经做了ostreama friend。我如何指示它访问maxLevels_数据成员?

跳过列表.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

标签: c++overloadingskip-lists

解决方案


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

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

推荐阅读