【发布时间】:2015-06-21 03:17:06
【问题描述】:
我有这段代码,但我想不出重载
ostream& operator<<(ostream& out)
{int check=0;
node *temp;
temp=this->head->next;
if(this->head->info==0)
out<<"- ";
while(temp!=NULL)
{ if(temp->info)
{out<<temp->info<<" ";
check=1;
temp=temp->next;}
else if(temp->info==0&&check==1)
{out<<temp->info<<" ";
temp=temp->next;}
else temp=temp->next;
}
return out;
}
我在课堂上有一个结构,并希望输出一个大数字。大数是用链表创建的。重载方法在类内部,我收到错误:当我使用
时,运算符 cout<< B;
在main里面。
有关上述代码的更多详细信息。检查变量是为了确保像 00100 这样的数字打印为 100。如果 head->info ==0 number 为负数,如果为 1,则 number 为正数。我从 head->next 开始,因为第一个节点有数字符号。
【问题讨论】:
-
operator<<重载不能是成员函数,如果您想要标准语法 - 您必须将其称为B << cout,这不是您所期望的。 -
显然 'B
标签: c++ linked-list operator-overloading