0

I'm following this tutorial to create a simple iterator, although they are iterating primitive int, I'm iterating an object type SpamValue.

I have class called SpamValue and another called SpamValueStackIter and they are tightly coupled, because I didn't want to expose a lot of getters, so I made one class SpamValueStackIter a "friend class" in SpamValue header.

#ifndef SPAMSTACK_H
#define SPAMSTACK_H

#include <iostream>
#include "SpamValue.h"
using namespace std; 

class SpamStack
{

public:
    friend class  SpamValueStackIter; 
    SpamStack(SpamValue** SpamValueItems, int size)
    {
        _position =-1; 
        _total_size = size;
        _SpamValueItems = new SpamValue*[_total_size];
         int i=0;
        for (; i<_total_size; i++)
        {
        this->_SpamValueItems[i] = SpamValueItems[i];
        }
    }
    ~SpamStack()
    {
         if (NULL!=_SpamValueItems)
                   {
                         /*delete each*/
                   int i =0;  
                   for  (; i<_total_size; i++)
                   {
                       if (NULL!=_SpamValueItems[i])
                       {
                            delete  _SpamValueItems[i];
                       }
                   }
                     /*delete the array*/
                    delete [] _SpamValueItems;
                   }
    }
    /*push*/
    void push(SpamValue* SpamValue)
    {
        _SpamValueItems[++_position];
    }
    /*pop*/
    SpamValue* pop()
    {
        return  _SpamValueItems[_position--];
    }
    /*isEmpty*/
    bool isEmpty()
    {
        return (_position == -1);
    }
    /*getters*/
    SpamValue** getSpamValueItems()
    {
        return  this->_SpamValueItems;  
    }
    int getTotalSize()
    {
        return _total_size; 
    }
     SpamValueStackIter* createIterator()const; 

private:
    SpamValue** _SpamValueItems; 
    int _total_size; 
    int _position; 


};


class SpamValueStackIter
{
    const SpamStack* _stack; 
    int _index; 

public:
    SpamValueStackIter(const SpamStack *s)
    {
        _stack = s; 

    }
    /*set index position to first item*/
    void first()
    {
        _index = 0; 
    }
    /*set index position to the next item in the iterator*/
    void next()
    {
        _index++; 
    }
    /*is the iteration completed */
    bool isDone()
    {
        return _index == _stack->_position + 1; 
    }
    /* return the current item */
    SpamValue* currentItem()
    {
        return _stack->_SpamValueItems[index];
    }

    /*create a new iterator*/
    SpamValueStackIter* SpamStack::createIterator()const
    {
        return new SpamValueStackIter(this);
    }



};
#endif /* SPAMSTACK_H*/

In the SpamStack.h:, Im getting this error:

SpamStack.h:77:6: error: ‘SpamValueStackIter’ does not name a type
      SpamValueStackIter* createIterator()const;

And also:

SpamStack.h:121:52: error: cannot define member function ‘SpamStack::createIterator                                                                                                             tor’ within ‘SpamValueStackIter’
     SpamValueStackIter* SpamStack::createIterator()const

Why can't SpamStack resolve the "friend class" that's defined in the same header?

After forward declaration, as suggested by others:

#ifndef SPAMSTACK_H
#define SPAMSTACK_H
#include <iostream>
#include "SpamValue.h"
using namespace std; 

/*forward declare*/
class  SpamValueStackIter; 
class SpamStack
{

public:
    friend class  SpamValueStackIter; 
    SpamStack(SpamValue** SpamValueItems, int size)
    {
        _position =-1; 
        _total_size = size;
        _SpamValueItems = new SpamValue*[_total_size];
         int i=0;
        for (; i<_total_size; i++)
        {
        this->_SpamValueItems[i] = SpamValueItems[i];
        }
    }
    ~SpamStack()
    {
         if (NULL!=_SpamValueItems)
                   {
                         /*delete each*/
                   int i =0;  
                   for  (; i<_total_size; i++)
                   {
                       if (NULL!=_SpamValueItems[i])
                       {
                            delete  _SpamValueItems[i];
                       }
                   }
                     /*delete the array*/
                    delete [] _SpamValueItems;
                   }
    }
    /*push*/
    void push(SpamValue* SpamValue)
    {
        _SpamValueItems[++_position];
    }
    /*pop*/
    SpamValue* pop()
    {
        return  _SpamValueItems[_position--];
    }
    /*isEmpty*/
    bool isEmpty()
    {
        return (_position == -1);
    }
    /*getters*/
    SpamValue** getSpamValueItems()
    {
        return  this->_SpamValueItems;  
    }
    int getTotalSize()
    {
        return _total_size; 
    }

    SpamValueStackIter* createIterator()const; 

private:
    SpamValue** _SpamValueItems; 
    int _total_size; 
    int _position; 


};

class SpamValueStackIter
{


public:
    SpamValueStackIter(const SpamStack *s)
    {
        _stack = s; 

    }
    /*set index position to first item*/
    void first()
    {
        _index = 0; 
    }
    /*set index position to the next item in the iterator*/
    void next()
    {
        _index++; 
    }
    /*is the iteration completed */
    bool isDone()
    {
        return _index == _stack->_position + 1; 
    }
    /* return the current item */
    SpamValue* currentItem()
    {
        return _stack->_SpamValueItems[index];
    }



private:
      const SpamStack* _stack;
      int _index; 


};

/create a new iterator/ SpamValueStackIter* SpamStack::createIterator()const { return new SpamValueStackIter(this); }

#endif /* SPAMSTACK_H */

In getting this error now:

SpamStack.h:117:45: error: invalid types ‘SpamValue** const[<unresolved overloaded function type>]’ for array subscript
         return _stack->_SpamValueItems[index];
user4581301
  • 33,082
  • 7
  • 33
  • 54
cyber101
  • 2,822
  • 14
  • 50
  • 93
  • 4
    You need to forward declare `class SpamValueStackIter;` *before* defining `class SpamStack`. – cigien May 06 '20 at 20:37
  • 1
    Mostly duplicate: [Why can't forward declared friend class be referred in the class?](https://stackoverflow.com/questions/52044848/why-cant-forward-declared-friend-class-be-referred-in-the-class). Asks a different question, but gets the exact same answer. – user4581301 May 06 '20 at 20:41
  • @cigien, thanks for advice however that did not work. Im getting undeclared/undefined errors now in SpamStack, after forward declaring it became a chicken or egg problem...I'm going to just declare these in separate classes & call it a day. – cyber101 May 06 '20 at 22:16
  • It did work. Unfortunately you had more bugs. Can't reproduce the new error. You may have left something out and your example does not exactly duplicate the code you are using. I get *'index' was not declared in this scope; did you mean '_index'?*, and when I follow the compiler's advice and use `_index` no error. A couple of compiler warnings in both cases. Definitely fix this one: *warning: value computed is not used [-Wunused-value]* over `_SpamValueItems[++_position];`. – user4581301 May 07 '20 at 05:54

0 Answers0