找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1781|回复: 1

拷贝构造函数怎么有问题?【有解了】

[复制链接]
发表于 2009-10-31 01:46:33 | 显示全部楼层 |阅读模式 IP:江苏扬州
//链表节点类型结构体模板头文件nodeType.h #ifndef H_nodeType #define H_nodeType template <class Type> struct nodeType { Type info; nodeType<Type> *back; nodeType<Type> *next; }; #endif //双向有序链表类模板头文件doubleLink.h #ifndef H_doubleLink #define H_doubleLink #include "nodeType.h" #include <iostream> using namespace std; template <class Type> class doubleLink { public: bool isEmpty(); void destroy(); void print(); void insertNode(const Type & insertItem); doubleLink(); doubleLink(const doubleLink<Type> & otherList); ~doubleLink(); private: nodeType<Type> *first; }; //判别链表是否为空 template <class Type> bool doubleLink<Type>::isEmpty() { return (first==NULL); } //销毁链表 template <class Type> void doubleLink<Type>::destroy() { nodeType<Type> *current; current=first; while(first!=NULL) { current=first; first=first->next; delete current; } } //打印链表 template <class Type> void doubleLink<Type>::print() { nodeType<Type> *current; if(first==NULL) cout<<"No items to print."; current=first; while(current!=NULL) { cout<<current->info<<" "; current=current->next; } cout<<endl; } //插入节点 template <class Type> void doubleLink<Type>::insertNode(const Type & insertItem) { nodeType<Type> *current; nodeType<Type> *newNode; nodeType<Type> *preCurrent; bool found; newNode=new nodeType<Type>; newNode->info=insertItem; newNode->back=NULL; newNode->next=NULL; if(first==NULL) first=newNode; else { //01 else found=false; current=first; while(current!=NULL && !found) if(current->info>=insertItem) found=true; else { preCurrent=current; current=current->next; } if(current==first) { first->back=newNode; newNode->next=first; first=newNode; } else { //00 else if(current!=NULL) { preCurrent->next=newNode; newNode->back=preCurrent; newNode->next=current; current->back=newNode; } else { preCurrent->next=newNode; newNode->back=preCurrent; } }//00 else }//01 else } //默认构造函数 template <class Type> doubleLink<Type>::doubleLink() { first=NULL; } //拷贝构造函数 template <class Type> doubleLink<Type>::doubleLink(const doubleLink<Type> & otherList) { nodeType<Type> *current; nodeType<Type> *newNode; nodeType<Type> *last=NULL; if(this!=&otherList) { //01 if if(first!=NULL) destroy(); if(otherList.first==NULL) first=NULL; else { //00 else current=otherList.first; first=new nodeType<Type>; first->info=current->info; first->next=NULL; first->back=NULL; last=first; current=current->next; while(current!=NULL) { //02 while newNode=new nodeType<Type>; newNode->info=current->info; newNode->next=NULL; newNode->back=NULL; last->next=newNode; newNode->back=last; last=newNode; current=current->next; } //02 while } //00 else } //01 if } //析构函数 template <class Type> doubleLink<Type>::~doubleLink() { destroy(); } #endif //测试程序testProgram.cpp #include "nodeType.h" #include "doubleLink.h" #include <iostream> using namespace std; int main() { const int N=11; doubleLink<int> testList; int m; cout<<"Please input "<<N-1<<" integers to build a double list:"<<endl; for(int i=0;i<N-1;i++) { cin>>m; testList.insertNode(m); } cout<<"The list you just build is:"; testList.print(); doubleLink<int> testList0(testList); //这里有问题! cout<<"After run copy constructor,testList0 is:"; testList0.print(); return 0; } 编译链接均没有错误,但是运行时拷贝构造函数就是不能执行,请高手们指点!另,我编了一个重载赋值运算符的函数,代码与本程序拷贝构造函数完全一样,但是重载的赋值运算符却执行的很好,郁闷! ---------------------------------------------------------- 挑战自己,超越自己,成就自己!
发表于 2009-10-31 01:46:34 | 显示全部楼层 IP:江苏扬州
自我发现: if(first!=NULL) // 这里的first 还没有初始化啊! destroy(); 这两行都注释掉 copy constructor可以假设被初始化的链表一开始就是空的至于这两行的用处嘛,可以在operator=里面显露出来。因为在做operator=操作之前,等号左边的doubleList对象里的first 要么是在default constructor里面被赋值为Null,要么是在copy constructor里面赋值了,总之first被初始化了。 --------------------------------------------------------- 挑战自己,超越自己,成就自己!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 15:18 , Processed in 0.376496 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表