title: Iterator pattern example giving error in c++
body: <pre><code>#include &lt;iostream&gt;
  using namespace std;
  class Stack
  {
      int items[10];
      int sp;
    public:
      friend class StackIter;
      Stack()
      {
          sp =  - 1;
      }
      void push(int in)
      {
          items[++sp] = in;
      }
      int pop()
      {
          return items[sp--];
      }
      bool isEmpty()
      {
          return (sp ==  - 1);
      }
      StackIter *createIterator() const // 2. Add a createIterator() member
  
  };
  
  class StackIter
  {
      // 1. Design an "iterator" class
      const Stack *stk;
      int index;
    public:
      StackIter(const Stack *s)
      {
          stk = s;
      }
      void first()
      {
          index = 0;
      }
      void next()
      {
          index++;
      }
      bool isDone()
      {
          return index == stk-&gt;sp + 1;
      }
      int currentItem()
      {
          return stk-&gt;items[index];
      }
  };
  
  StackIter *Stack::createIterator() const
  {
    return new StackIter(this);
  }
  
  bool operator == (const Stack &amp;l, const Stack &amp;r)
  {
    // 3. Clients ask the container object to create an iterator object
    StackIter *itl = l.createIterator();
    StackIter *itr = r.createIterator();
    // 4. Clients use the first(), isDone(), next(), and currentItem() protocol
    for (itl-&gt;first(), itr-&gt;first(); !itl-&gt;isDone(); itl-&gt;next(), itr-&gt;next())
      if (itl-&gt;currentItem() != itr-&gt;currentItem())
        break;
    bool ans = itl-&gt;isDone() &amp;&amp; itr-&gt;isDone();
    delete itl;
    delete itr;
    return ans;
  }
  
  int main()
  {
    Stack s1;
    for (int i = 1; i &lt; 5; i++)
      s1.push(i);
    Stack s2(s1), s3(s1), s4(s1), s5(s1);
    s3.pop();
    s5.pop();
    s4.push(2);
    s5.push(9);
    cout &lt;&lt; "1 == 2 is " &lt;&lt; (s1 == s2) &lt;&lt; endl;
    cout &lt;&lt; "1 == 3 is " &lt;&lt; (s1 == s3) &lt;&lt; endl;
    cout &lt;&lt; "1 == 4 is " &lt;&lt; (s1 == s4) &lt;&lt; endl;
    cout &lt;&lt; "1 == 5 is " &lt;&lt; (s1 == s5) &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Compilation Error :</p>
  
  <pre><code>main.cpp:25:5: error: 'StackIter' does not name a type
       StackIter *createIterator()const // 2. Add a createIterator() member
  main.cpp: In function 'bool operator==(const Stack&amp;, const Stack&amp;)':
  main.cpp:68:22: error: 'const class Stack' has no member named 'createIterator'
     StackIter *itl = l.createIterator();
  main.cpp:69:22: error: 'const class Stack' has no member named 'createIterator'
     StackIter *itr = r.createIterator();
  </code></pre>
  
  <p>What am I doing wrong?</p>