title: Implement range-based for loop of a Stack based on linked-list
body: <p>I am making a <code>Stack</code> which is based on <code>linked-list</code>. Everything works fine except that I don't know how to implement a "<a href="https://en.cppreference.com/w/cpp/language/range-for" rel="nofollow noreferrer">range-based for loop</a>" for this one.</p>
  
  <p>I got the <code>error: no match for ‘operator++’ (operand type is ‘Stack&lt;int&gt;::Node’)</code>. </p>
  
  <p>What's wrong and how can I fix it?</p>
  
  <hr>
  
  <p>Code (Foolish me overloads post <code>++</code> not prefix <code>++</code>, now all corrected.):</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  template&lt;typename T&gt;
  class Stack{
  private:
      class Node{
          friend Stack;
      public:
          void operator++(){
              this-&gt;next = this-&gt;next-&gt;next;   //point to next Node
          }
  
          bool operator!=(const Node&amp; rhs){
              return !(*this == rhs);
          }
  
          T operator*(){
              return this-&gt;next-&gt;elem;  //return current Node elem
          }
  
          bool operator==(const Node&amp; rhs){
              return this-&gt;next == rhs.next;
          }
  
      private:
          T elem;
          Node* next;
  
      };
  
      Node* first;
      int _size;
  
  public:
      Stack():_size(0){
          first = nullptr;
      }
  
      void push(T item){
          Node* n = new Node;
          n-&gt;elem = item;
          n-&gt;next = first;
          first = n;
          _size++;
      }
  
      T pop(){
          T item = first-&gt;elem;
          Node* old_first = first;
          first = first-&gt;next;
          delete old_first;
          _size--;
          return item;
      }
  
      int size(){
          return _size;
      }
  
      bool empty(){
          return _size == 0;
      }
  
      Node begin(){
          Node n;
          n.next = first;
          return n;
      }
  
      Node end(){
          Node m;
          m.next = nullptr;
          return m;
      }
  
      ~Stack(){
          Node* ele_to_delete;
          while(first != nullptr){
              ele_to_delete = first;
              first = first-&gt;next;
              delete ele_to_delete;
          }
      }
      Stack(const Stack&amp;) = delete;
      Stack&amp; operator=(const Stack&amp;) = delete;
  };
  
  
  int main(){
      Stack&lt;int&gt; ls;
      ls.push(1);
      ls.push(2);
      ls.push(3);
      for(auto s: ls){
          cout &lt;&lt; s &lt;&lt; "|";
      }
      return 0;
  }
  </code></pre>