| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 702 人关注过本帖
标题:拷贝构造函数怎么有问题?【有解了】
只看楼主 加入收藏
shan183
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-5-30
收藏
 问题点数:0 回复次数:1 
拷贝构造函数怎么有问题?【有解了】
//链表节点类型结构体模板头文件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; } 编译链接均没有错误,但是运行时拷贝构造函数就是不能执行,请高手们指点! 另,我编了一个重载赋值运算符的函数,代码与本程序拷贝构造函数完全一样,但是重载的赋值运算符却执行的很好,郁闷! ---------------------------------------------------------- 挑战自己,超越自己,成就自己!

[此贴子已经被作者于2005-6-19 11:38:57编辑过]

搜索更多相关主题的帖子: 函数 构造 拷贝 
2005-06-17 15:08
shan183
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-5-30
收藏
得分:0 
自我发现: if(first!=NULL) // 这里的first 还没有初始化啊! destroy(); 这两行都注释掉 copy constructor可以假设被初始化的链表一开始就是空的 至于这两行的用处嘛,可以在operator=里面显露出来。因为在做operator=操作之前,等号左边的doubleList对象里的first 要么是在default constructor里面被赋值为Null,要么是在copy constructor里面赋值了,总之first被初始化了。 --------------------------------------------------------- 挑战自己,超越自己,成就自己!

[此贴子已经被作者于2005-6-19 11:36:38编辑过]

2005-06-19 11:36
快速回复:拷贝构造函数怎么有问题?【有解了】
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018824 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved