跪求各位大神解决呀!!仍旧是析构函数问题!!
本帖最后由 zfq_jxj 于 2013-01-04 17:09:13 编辑
#include<iostream>
#include<fstream>
using namespace std;
template<class T>
struct LinkedQueueNode //队列的结点
{
T element;
LinkedQueueNode<T> *next;
LinkedQueueNode()
{
next=NULL;
}
};
template<class T>
class Queue //一个链式队列
{
public:
Queue();
~Queue();
bool EnterQueue(const T &data);
bool Delete(T &data);
private:
int size;
LinkedQueueNode<T> *font;
LinkedQueueNode<T> *rear;
};
template<class T>
Queue<T>::Queue()
{
size=0;
font=new LinkedQueueNode<T>;
font->next=NULL;
rear=NULL;
}
template<class T>
Queue<T>::~Queue()
{
delete font;
}
template<class T>
bool Queue<T>::EnterQueue(const T &data)
{
LinkedQueueNode<T> *p=new LinkedQueueNode<T>;
p->element=data;
if(p==NULL) return false;
if(size==0)
{
font->next=p;
rear=p;
size++;
return true;
}
rear->next=p;
rear=p;
size++;
return true;
}
template<class T>
bool Queue<T>::Delete(T &data)
{
if(size==0) return false;
LinkedQueueNode<T> *del=NULL;
del=font->next;
data=del->element;
font->next=del->next;
delete del;
return true;
}
#define file1 "AirLine.bin"
class Passager
{
public:
Passager()
{
ticket_num=0;
seat_num=NULL;
}
Passager(int t_num,int *s_num)
{
ticket_num=t_num;
if(s_num!=NULL)
{
seat_num=new int[ticket_num];
for(int i=0;i<ticket_num;i++)
seat_num[i]=s_num[i]; //把座位号逐个赋值
}
}
Passager(Passager &p)
{
this->ticket_num=p.ticket_num;
this->seat_num=new int[p.ticket_num];
for(int i=0;i<ticket_num;i++)
seat_num[i]=p.seat_num[i];
}
~Passager()
{
if(seat_num!=NULL)//到这里就有问题了!!显示:Error :expression cannot be evaluated
delete []seat_num;
}
bool operator==(const Passager &p)const //
{
int i;
for(i=0;i<p.ticket_num;i++) //判断每个座位号是否相同
if(this->seat_num[i]!=p.seat_num[i])
return false;
else continue;
return (ticket_num==p.ticket_num&&i==p.ticket_num);
}
Passager &operator=(const Passager &p)
{
ticket_num = p.ticket_num;
if ( seat_num != NULL )
delete [] seat_num;
seat_num=new int[ticket_num];
for(int i=0;i<ticket_num;i++)
seat_num[i]=p.seat_num[i];
return *this;
}
private:
int ticket_num; //订票额
int *seat_num; //座位号,这里相当于有ticket_num个座号
};
class AirLine
{
public:
AirLine();
~AirLine();
void InputAirLine(AirLine &A_L);
void WriteAirLine();
void ReadAirLine();
private:
char start_city[30];
Queue<Passager> bookedpassager; //这里的航线类我加了一个客人队列,我想把队列的头结点给保存起来,
};
AirLine::AirLine()
{
start_city[0]=NULL; //先进入Queue<>的构造函数
}
AirLine::~AirLine()
{}
void AirLine::InputAirLine(AirLine &A_L)
{
cout<<"城市名:";
cin>>A_L.start_city;
}
void AirLine::WriteAirLine()
{
ofstream fout(file1,ios_base::out|ios_base::app|ios_base::binary);
if(fout.fail())
{
cout<<"文件"<<file1<<"打开失败!"<<endl;
exit(1);
}
AirLine A_L;
A_L.InputAirLine(A_L); //我是把一个AirLine对象保存在文件file1中,
fout.write((char *)&A_L,sizeof A_L);
fout.close();
}
void AirLine::ReadAirLine()
{
AirLine A_L;
ifstream fin(file1,ios_base::in|ios_base::binary);
if(fin.fail())
{
cout<<"文件"<<file1<<"打开失败!"<<endl;
exit(1);
}
while(fin.read((char *)&A_L,sizeof A_L)) //但读取AirLine对象时就出问题了,和以前一样也是析构函数的问题,
//调试发现A_L.start_city前后地址值没变,但font的地址值变了
cout<<A_L.start_city<<endl;
fin.close();
}
int main()
{
AirLine A;
//A.WriteAirLine(); //写入没问题
A.ReadAirLine(); //读出来城市名后就有问题了!!
return 0;
}