问题描述
所以我使用C ++编写代码已经有很长时间了,并且在复制构造函数方面苦苦挣扎,使用它时我的程序不断崩溃。
节点:
template <class T>
class Node
{
public:
Node(T data,Node<T> * n = 0)
{ element = data;
next = n;
prev = n;}
~Node()
{ next = 0;
prev = 0;}
T element;
Node<T>* next;
Node<T>* prev;
功能代码:
CircularList<T>::CircularList(const CircularList<T> & other)
{
if(other.tail == NULL)
{
this->tail = NULL;
}
else
{
this->tail = other.tail;
Node<T> * original = this->tail;
Node<T> * temp = other.tail->next;
while(temp != other.tail)
{
original->next = temp;
temp = temp->next;
original = original->next;
}
original->next = temp;
}
}
(编辑) 这是CircularList类的规范。我想我只是不知道要采取什么步骤来使它成为深拷贝而不是浅拷贝。
template<class T>
class CircularList;
template<class T>
ostream& operator<<(ostream&,CircularList<T>&);
template<class T>
class CircularList : public LinearStructure<T>
{
public:
friend ostream& operator<< <T>(ostream&,CircularList<T>&);
CircularList();
CircularList(const CircularList<T>& other);
CircularList<T>& operator=(const CircularList<T>& other);
virtual CircularList<T>* clone();
virtual ~CircularList();
virtual void insert(int index,T element);
virtual T remove(int index);
virtual T get(int index) const;
virtual bool isEmpty();
virtual void clear();
Node<T>* getLeader();
protected:
ostream& print(ostream& os);
private:
int size() const;
Node<T>* tail;
};
是的,当我对此进行测试时(列表不为空):
CircularList<int> clist;
clist.insert(0,8);
CircularList<int> clist2(clist);
它崩溃了。任何帮助,将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)