重载<<(代码错误)

Overloading << (Error with code)

本文关键字:lt 错误 重载 代码      更新时间:2023-10-16

有没有人能告诉我<<操作符。Visual studio告诉我有很多参数。Node是一个结构

ostream& operator << (ostream& strm, const node* &obj )
{
    strm << "ItdID: " << obj.itemId << "nItem Description: " << obj.itemDescription << "nItem Quanity: " << obj.quanity 
         << "nWholesale Cost: $" << fixed << showpoint << setprecision(2) << obj.wholesale << "nRetail Cost: $" << fixed << showpoint << setprecision(2) << obj.retail;
    return strm;
}

您需要将ostream& operator << (ostream& strm, const node* &obj )声明为friend函数到node类。

那么在你的节点类中你要有

friend ostream& operator << (ostream& strm, const node* &obj );

然后在你的CPP文件中有

ostream& operator << (ostream& strm, const node* &obj )
{
    strm << "ItdID: " << obj.itemId << "nItem Description: " << obj.itemDescription << "nItem Quanity: " << obj.quanity 
         << "nWholesale Cost: $" << fixed << showpoint << setprecision(2) << obj.wholesale << "nRetail Cost: $" << fixed << showpoint << setprecision(2) << obj.retail;
    return strm;
}