求助 重载+运算符

来源:百度知道 编辑:UC知道 时间:2024/06/01 22:00:18
请问下面的程序错在哪里?为什么会出现异常?
#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(Set &);
~Set(){make_empty();delete first;}
void make_empty();
void print();
void create();
bool add(const char);
Set operator+(const Set &);
};

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

Set::Set(Set &r)
{
set *p1=r.first->next;
set *p2=first=new set;
while(p1)
{
p2->next=new set;
p2=p2->next;
p2->data=p1->data;
p1=p1->next;
}
p2->next=0;
last=p2;
}

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

#include"stdafx.h"
#include"iostream"
using namespace std;

struct set
{
char data;
set *next;
set(){
next=0;
}
};

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

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

Set::Set(Set &r)
{
set *p1=r.first->next;
set *p2=first=new set;
while(p1)
{
p2->next=new set;
p2=p2->next;
p2->data=p1->data;
p1=p1->next;