拷贝构造函数怎么有问题?【有解了】
//链表节点类型结构体模板头文件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编辑过]