我試圖超載< <運算符,所以我可以輸出鏈表的內容。Overloading << C++
所以,如果我與值1,2,3一鏈表和我稱:
cout << list << endl;
其中列表的類型是鏈表的。我應該得到這樣的東西:
1 2 3或1 | 2 | 3在終端。
我應該使用循環遍歷列表並將每個節點的數據添加到輸出?想實現,這並不需要看到節點類的方法...
ostream& operator <<(ostream& outs, const LinkedList& source)
{
//use a loop to cycle through, adding each data to outs, seperated by a space or a |
return outs;
}
謹以此制定出一個結構好嗎?:
LinkedList::report(ostream& outs) //thanks beta :)
{
Node *currentPtr = headPtr;
while(currentPtr != NULL)
{
outs << currentPtr->getData() << " "; //thanks Rob :)
currentPtr = currentPtr->getNextPtr();
}
return outs;
}
你可能想看看[this](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers)(; – Mankarse
它是功課嗎? – amit
你的'LinkedList'類是模板嗎? –