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();
}
你需要得到一本不同的書。這段代碼很糟糕。 – Mankarse