| 
        
             下面是一个同学的程序。调试没问题,但执行时错了。同学程序链接:http://blog.csdn.net/miumor/article/details/40891645  错误信息是:     上面内容拷自学生原来博文,一看错误是析构函数有问题。但程序广编译没问题,是什么原因呢?细看程序:析构函数在类的声明中有声明,但在后面没写代码定义。大家还记得吗,你们一定学过,析构函数的特点:系统会给我们定义一个的,但是如果我们自己程序中声明了,系统就不会定义了。但当你自己只声明,不定义代码,而系统的又不给定义,析构函数就空了。所在改的方法就是:删除析构函数的声明,或者写出其代码,在这里显然是要写代码更合理些(你明白是为什么)。老师,有个错误我不知道怎么改。。
 
 
   
 
  #include<iostream>using namespace std;
 const int QSize=100;
 template<class Datatype>
 struct Node
 {Datatype data;
 Node<Datatype> *next;
 };
 template<class Datatype>
 class linkqueue
 {public:
 linkqueue();
 ~linkqueue();
 void en(Datatype x);
 Datatype chu();
 Datatype get();
 int empty();
 private:
 Node<int>*front,*rear;
 };
 template<class Datatype>
 linkqueue<Datatype>::linkqueue()
 {Node<Datatype>*s=NULL;
 s=new Node<Datatype>;
 s->next=NULL;
 front=rear=s;
 }
  
  template<class Datatype> ? ? ? ? ? ? ? ? ? ? ?linkqueue<Datatype>::~linkqueue() ? ?//调试修正方案:这个函数代码是要加上的。
 {? Node<Datatype>*p=NULL;
 ? while(front!=NULL)
 ?p=front->next;
 ? delete front;
 ? front=p;
 }
 
  
  template<class Datatype>void linkqueue<Datatype>::en(Datatype x)
 {Node<Datatype>*s=NULL;
 s=new Node<Datatype>;
 s->data=x;
 s->next=NULL;
 rear->next=s;rear=s;
 }
 template<class Datatype>
 Datatype linkqueue<Datatype>::chu()
 {Node<Datatype>*p=NULL;
 int x;
 if(rear=front)throw"下溢";
 p=front->next;
 x=p->data;
 front->next=p->next;
 if(p->next=NULL)rear=front;
 delete p;
 return x;
 }
 
 template<class Datatype>
 Datatype linkqueue<Datatype>::get()
 {if(front!=rear)
 ? ? return front->next->data;
 }
 template<class Datatype>
 int linkqueue<Datatype>::empty()
 {if(front==rear)
 return 1;
 else
 return 0;
 }
 void main()
 {linkqueue<int>Q;
 if(Q.empty())
 cout<<"队列为空"<<endl;
 else
 cout<<"对15和10执行入队操作"<<endl;
 try
 {Q.en(15);
 Q.en(10);
 }
 catch(char*wrong)
 {cout<<wrong<<endl;
 }
 cout<<"查看队头元素"<<endl;
 cout<<Q.get()<<endl;
 cout<<"执行出队操作"<<endl;
 try
 {Q.chu();
 }
 ? ? catch(char*wrong)
 {cout<<wrong<<endl;
 }
 cout<<"查看队头元素"<<endl;
 cout<<Q.get()<<endl;
 }
 
 
 
 (编辑:宣城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |