求助 重载运算符问题

来源:百度知道 编辑:UC知道 时间:2024/06/25 03:48:34
下面一个程序出现异常,错在哪里?该怎么修改?

#include"iostream"
using namespace std;

struct set
{
char data;
set *next;
};

class Set
{
set *first,*last;
public:
Set(){first=last=new set;first->next=0;}
~Set(){make_empty();delete first;}
void make_empty();
void print();
void create();
bool add(const char);
Set operator=(Set &);
};

int main()
{
Set s1,s2,s3;
s1.create();
s2=s1;
s2.print();
return 0;
}

void Set::make_empty()
{
set *p1=first->next,*p2;
while(p1)
{
p2=p1;
p1=p1->next;
delete p2;
}
last=first;
first->next=0;
}

void Set::print()
{
set *p=first->next;
for(;p;p=p->next)
cout<<p->data<<' ';
cout<<endl;
}

void Set::create()
{
cout&

别的没看,一眼就看见这个赋值操作符就不对,应该返回引用Set& Set::operator=(const Set &r)

应为Set& operator=(Set &),其他部分不变,否则编译器会构造一个临时对象