【问题标题】:Assignment operator linked list c++赋值运算符链表c ++
【发布时间】: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


【解决方案1】:

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

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

应该是:

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

【讨论】:

  • 有道理,谢谢。我不清楚函数声明会是什么样子。目前是这样的:
  • 布尔运算符 = (const SortedLinkList& otherList);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
相关资源
最近更新 更多