【发布时间】:2015-04-07 22:24:48
【问题描述】:
我正在尝试为 C++ 中的链表类编写赋值运算符。我得到的错误说“头”是未声明的,但我不确定我应该在哪里声明它。它可以毫无问题地用于其他功能。另一个错误说我对“this”的使用无效。
template <class T>
SortedLinkList<T>& operator=(const SortedLinkList<T> & otherList)
{
if(&otherList != this) {
Node<T> temp = head;
while(temp->getNext() != NULL) {
head = head -> getNext();
delete temp;
temp = head;
}
count = 0;
temp = otherList.head;
while(temp != NULL) {
insert(temp);
}
}
return *this;
}
【问题讨论】:
-
这是在类声明之内还是之外?
-
在类声明之外
标签: c++ linked-list overloading assignment-operator