Home > Software engineering >  C friend keyword not accessing non-static data member
C friend keyword not accessing non-static data member

Time:11-04

I need to overload the ostream operator with new functionality for a doubly linked Skip List class.

When I cout the instance of my class, I want it to iterate through my the levels of my skip list, and wherever the head pointer is pointed to a nullptr I want it to print the level name and a status of empty.

Would look something like:

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

I need the number of levels to be dynamically entered. I try to assign int level = SkipList::maxLevels_; but I get the error invalid use of non-static data member

I've made the ostream a friend. How can I instruct it access the maxLevels_ data member?

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

CodePudding user response:

SkipList::maxLevels_; refers to the static maxLevels_ member of the SkipList class. So, if you need maxLevels_ to be the maximum level of all the instances of SkipList you have to declare it as static. Otherwise in your overloaded friend function you have to use the private member of the list instance.

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