赋值运算符链表c ++

时间:2015-02-07 22:54:36

标签: c++ linked-list overloading assignment-operator

我正在尝试为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;
}

1 个答案:

答案 0 :(得分:1)

this指针不可用,因为您的函数签名与成员定义不相似,您缺少签名的类型范围解析部分:

template <class T>
SortedLinkList<T>& operator=(const SortedLinkList<T> & otherList)

应该是:

template <class T>
SortedLinkList<T>& SortedLinkList<T>::operator=(const SortedLinkList<T> & otherList)
相关问题