2015-04-21 189 views
0

我正在创建测试代码以显示C++中链接列表类的实现。大多数情况下,所提供的所有代码都是从我为主函数编写的书以及用于添加尾部实例的一些编辑过的行之外的书中给出的。这正显示出,现在当我编译了唯一的错误是这样的:'LList :: operator =':必须返回一个值

'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(); 
} 
+2

你需要得到一本不同的书。这段代码很糟糕。 – Mankarse

回答

0

在那里你有这样的代码:

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=的情况下,您最常想要返回对象本身。

相关问题