0
我試着去調用該函數:流插入重載C++
friend ostream& operator<<(ostream& out, stack::myItem& theItem);
這是公衆對我的堆棧對象:
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);
};
我所知道的是,下面這個函數:
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;
}
給出的聲明: 堆棧s =堆棧(7); 上面的函數每當我使用時調用: cout < < s;
如何調用下面的函數?
ostream& operator<<(ostream& out, stack::myItem& theItem)
out << theItem.item;
return out;
}
因爲當我試圖做到以下幾點:
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;
}
它導致崩潰,因爲聲明出來< <秒;將層出不窮。而調試代碼永遠不會進入下一個發言...
你能否進一步闡述終止conditon報表? – user40120 2009-10-10 05:39:44
如果你的意思是最後的註釋,循環中沒有條件的代碼更清楚地表明意圖;你用下面的逗號打印出元素0- [end-1],然後用換行符打印出最後一個元素 – 2009-10-10 05:54:10