Stream Insertion Overloading C++
<p>Im trying to call the function:</p>

<pre><code>friend ostream&amp; operator&lt;&lt;(ostream&amp; out, stack::myItem&amp; theItem);
</code></pre>

<p>that is public to my stack object:</p>

<pre><code>class stack 
{ 
public: 
    stack(int capacity); 
    ~stack(void); 
     void method1(); 
     ... 

 private: 

    struct myItem 
    { 
    int             item; 
}; 

  ... 
public: 
    friend ostream&amp; operator&lt;&lt;(ostream&amp; out, stack&amp; s); 
    friend ostream&amp; operator&lt;&lt;(ostream&amp; out, stack::myItem&amp; theItem); 
};
</code></pre>

<p>What I know is that this function below:</p>

<pre><code>ostream&amp; operator&lt;&lt;(ostream&amp; out, stack&amp; s)
{
 if ( s.count == 0 ) // then no elements have been counted.
    out &lt;&lt; "\nstack: empty\n\n";
else
        {
        out &lt;&lt; "\nstack: ";
        for ( int i = 0; i &lt; s.count; i++ )
        {
   if ( i &lt; s.count-1 )
                     out &lt;&lt; s.myItem[i].item &lt;&lt; ", ";
                else out &lt;&lt; s.myItem[i].item;
        }
        out &lt;&lt; "\n\n";
 }
    return out;
}
</code></pre>

<p>given the statement:
    stack s = stack(7);
the function above is called whenever i use: 
    cout &lt;&lt; s;</p>

<p>How do I call the function below?</p>

<pre><code>ostream&amp; operator&lt;&lt;(ostream&amp; out, stack::myItem&amp; theItem)

 out &lt;&lt; theItem.item;
 return out;
}
</code></pre>

<p>Because when I try to do the following:</p>

<pre><code>ostream&amp; operator&lt;&lt;(ostream&amp; out, stack&amp; s)
{
if ( s.count == 0 ) // then no elements have been counted.
        out &lt;&lt; "\nstack: empty\n\n";
else
        {
        out &lt;&lt; s;   
        }
        return out;
}
</code></pre>

<p>It results in a crash, because the statement out &lt;&lt; s; will be endless.. While debugging the code will never go to the next statement...</p>
