c++ - 'List::operator=': 必须返回一个值

标签 c++ return return-value operator-keyword

我正在做一项作业,我在其中创建测试代码以展示 C++ 中链表类的实现。大多数提供的所有代码都来自一本书,而不是我为我的 main 函数编写的代码和一些编辑的代码行以添加尾部实例。我编译时出现的唯一错误是:

    'LList::operator=' : must return a value

我不确定为什么会产生这个错误或如何修复它,因为它引用了书中给出的一段代码。该代码块是:

    LList& LList::operator=(const LList& source)
    {
        dealloc();
        copy(source);
    }

如有任何帮助,我们将不胜感激。 万一;这是我的源文件中的其余代码。

// LList.cpp
#include "LList.h"
#include <iostream>

LList::LList()
{
    head_ = NULL;
    tail_ = NULL;
    size_ = 0;
}

ListNode* LList::_find(size_t position)
{
    ListNode *node = head_;
    size_t i;

    for (i = 0; i<position; i++) {
        node = node->link_;
    }
    return node;
}

ItemType LList::_delete(size_t position)
{
    ListNode *node, *dnode;
    ItemType item;

    if (position == 0) {
        dnode = head_;
        head_ = head_->link_;
        item = dnode->item_;
        delete dnode;
    }
    if (position == size_) {
        dnode = tail_;
        node = _find(position - 1);
        node->link_ = NULL;
        tail_ = node;
        item = dnode->item_;
        delete dnode;
    }
    else {
        node = _find(position - 1);
        if (node != NULL) {
            dnode = node->link_;
            node->link_ = dnode->link_;
            item = dnode->item_;
            delete dnode;
        }
    }
    size_ -= 1;
    return item;
}

void LList::append(ItemType x)
{
    ListNode *node, *newNode = new ListNode(x);

    if (head_ != NULL) {
        node = _find(size_ - 1);
        node->link_ = newNode;
        tail_ = newNode;
    }
    else {
        head_ = newNode;
        tail_ = head_;
    }
    size_ += 1;
}

void LList::insert(size_t i, ItemType x)
{
    ListNode *node;

    if (i == 0) {
        head_ = new ListNode(x, head_);
        tail_ = head_;
    }
    else if (i == size_) {
        tail_ = new ListNode(x, tail_);
    }
    else {
        node = _find(i - 1);
        node->link_ = new ListNode(x, node->link_);
    }
    size_ += 1;
}

void LList::printlist()
{
    ListNode *temp = head_;

    while (temp) {
        std::cout << temp->item_ << std::endl;
        temp = temp->link_;
    }
}


ItemType LList::pop(int i)
{
    if (i == -1) {
        i = size_ - 1;
    }
    return _delete(i);
}

ItemType& LList::operator[](size_t position)
{
    ListNode *node;

    node = _find(position);
    return node->item_;
}

LList::LList(const LList& source)
{
    copy(source);
}

void LList::copy(const LList &source)
{
    ListNode *snode, *node;

    snode = source.head_;
    if (snode) {
        node = head_ = new ListNode(snode->item_);
        snode = snode->link_;

        while (snode) {
            node->link_ = new ListNode(snode->item_);
            node = node->link_;
            snode = snode->link_;
        }
    }
    size_ = source.size_;
}

LList& LList::operator=(const LList& source)
{
    dealloc();
    copy(source);
}

LList::~LList()
{
    dealloc();
}

void LList::dealloc()
{
    ListNode *node, *dnode;

    node = head_;
    while (node) {
        dnode = node;
        node = node->link_;
        delete dnode;
    }



}

int main()
{
    LList mylist;

    mylist.append(6);
    mylist.append(3);
    mylist.append(1);
    mylist.printlist();
    mylist.insert(2, 4);
    mylist.printlist();
    mylist.pop(1);
    mylist.printlist();
    mylist.size();
}

最佳答案

你有这段代码的地方:

LList& LList::operator=(const LList& source)
{
    dealloc();
    copy(source);
}

改成这样:

LList& LList::operator=(const LList& source)
{
    dealloc();
    copy(source);
    return *this; // Note the addition of this return statement.
}

解释:

在这里,您要覆盖 LList 上的 = 运算符,而在 C++ 中,运算符覆盖必须返回一个值。至少在 operator= 的情况下,您通常只想返回对象本身。

关于c++ - 'List::operator=': 必须返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785184/

相关文章:

c++ - 在 Linux 上将 CodeLite 和 OpenGL 与 freeglut 链接起来

c++ - VC2012错误C2676,VC2005错误C2784 : reverse_iterator for map of template classes

c++ - 我如何才能始终强制包含标题?

c - 从非 void 函数的末尾掉落时写入未使用的参数的返回值

c++ - 为什么这个 std::bind 没有转换为 std::function?

c++ - 如何突破一个函数

php - MySql 不返回任何内容

调用窗口对象上的 javascript 函数会替换该窗口的内容吗? (有时??)

c# - 通过catch block 从函数返回,finally block 会发生什么?

c++ - 为什么三元运算符在我的代码中不起作用?