Im trying to call the function:
friend ostream& operator<<(ostream& out, stack::myItem& theItem);
that is public to my stack object:
class stack
{
public:
stack(int capacity);
~stack(void);
void method1();
...
private:
struct myItem
{
int item;
};
...
public:
friend ostream& operator<<(ostream& out, stack& s);
friend ostream& operator<<(ostream& out, stack::myItem& theItem);
};
What I know is that this function below:
ostream& operator<<(ostream& out, stack& s)
{
if ( s.count == 0 ) // then no elements have been counted.
out << "\nstack: empty\n\n";
else
{
out << "\nstack: ";
for ( int i = 0; i < s.count; i++ )
{
if ( i < s.count-1 )
out << s.myItem[i].item << ", ";
else out << s.myItem[i].item;
}
out << "\n\n";
}
return out;
}
given the statement: stack s = stack(7); the function above is called whenever i use: cout << s;
How do I call the function below?
ostream& operator<<(ostream& out, stack::myItem& theItem)
out << theItem.item;
return out;
}
Because when I try to do the following:
ostream& operator<<(ostream& out, stack& s)
{
if ( s.count == 0 ) // then no elements have been counted.
out << "\nstack: empty\n\n";
else
{
out << s;
}
return out;
}
It results in a crash, because the statement out << s; will be endless.. While debugging the code will never go to the next statement...