C++ 测试C++;链接实现错误

C++ 测试C++;链接实现错误,c++,linked-list,C++,Linked List,我正在尝试测试教授给我们的代码。我们必须改变代码的实现,但这不是我一直坚持的。我一直在做一个有效的测试代码。他给了我要运行的测试代码,但当我尝试运行它时,我不断地得到错误,这本不应该是这样的。有谁能告诉我测试代码中的问题是什么,这样我就可以开始更改并向代码中添加不同的函数了?谢谢 这是我的密码: // ListNode.h #ifndef _LISTNODE_H #define _LISTNODE_H #include <cstdlib> typedef int ItemType

我正在尝试测试教授给我们的代码。我们必须改变代码的实现,但这不是我一直坚持的。我一直在做一个有效的测试代码。他给了我要运行的测试代码,但当我尝试运行它时,我不断地得到错误,这本不应该是这样的。有谁能告诉我测试代码中的问题是什么,这样我就可以开始更改并向代码中添加不同的函数了?谢谢

这是我的密码:

// ListNode.h
#ifndef _LISTNODE_H
#define _LISTNODE_H

#include <cstdlib>

typedef int ItemType;

class ListNode {
    friend class LList;

public:
    ListNode(ItemType item, ListNode* link = NULL);

private:
    ItemType item_;
    ListNode *link_;
};

inline ListNode::ListNode(ItemType item, ListNode *link)
{
    item_ = item;
    link_ = link;
}

#endif // _LISTNODE_H

//LList.cpp
#包括“LList.h”
LList::LList()
{
头=空;
尺寸=0;
}
ListNode*List::\u查找(大小\u位置)
{
ListNode*node=头部;
尺寸i;
对于(i=0;ilink_2;;
}
返回节点;
}
ItemType LList::\u删除(大小\u位置)
{
ListNode*node,*dnode;
项目类型项目;
如果(位置==0){
dnode=头部;
头部=头部->链接;
item=dnode->item\ux;
删除dnode;
}
否则{
节点=_查找(位置-1);
如果(节点!=NULL){
dnode=节点->链接;
节点->链接=节点->链接;
item=dnode->item\ux;
删除dnode;
}
}
尺寸u-=1;
退货项目;
}
void LList::append(ItemType x)
{
ListNode*node,*newNode=newlistnode(x);
if(head.=NULL){
node=\u find(大小为-1);
节点->链接=新节点;
}
否则{
头部=新节点;
}
尺寸u1+=1;
}
void LList::insert(大小i,项目类型x)
{
ListNode*节点;
如果(i==0){
head_uu=新列表节点(x,head_uu);
}
否则{
node=_find(i-1);
节点->链接=新建列表节点(x,节点->链接);
}
尺寸u1+=1;
}
ItemType-LList::pop(inti)
{
如果(i==-1){
i=尺寸_u1;
}
返回-删除(i);
}
ItemType&LList::运算符[](大小和位置)
{
ListNode*节点;
节点=_查找(位置);
返回节点->物料;
}
LList::LList(const-LList&source)
{
副本(来源);
}
void-LList::复制(const-LList和source)
{
ListNode*snode,*node;
snode=source.head\uux;
if(snode){
节点=头节点=新建列表节点(节点->项目节点);
snode=snode->link\ux;
while(snode){
节点->链接=新建列表节点(节点->项目);
节点=节点->链接;
snode=snode->link\ux;
}
大小=source.size;
}
LList&LList::operator=(const-LList&source)
{
if(此!=&source){
dealoc();
副本(来源);
}
归还*这个;
}
利斯特::~LList()
{
dealoc();
}
void-LList::dealloc()
{
ListNode*node,*dnode;
节点=头;
while(节点){
dnode=节点;
节点=节点->链接;
删除dnode;
}
}

#包括“LList.h”
#包括
使用名称空间std;
int main()
{
利斯特b,c;
int x;
b、 追加(1);
b、 追加(2);
b、 追加(3);
c、 追加(4);
c、 追加(5);
c=b;
x=b.pop();
cout在您的代码中:


cout您正试图输出您的
LList
,但这并不是那么简单。您需要重载输出运算符:

friend std::ostream& operator<< (std::ostream& stream, const LList& list) {
    // Write code here to iterate through the list
    // and output each item...

    // Return the stream so we can chain outputs..
    return stream;
}

friend std::ostream&operator您还没有为
LList
类定义插入器。仅供参考,
LList::copy
似乎在某个地方缺少其结尾
}
。“如果您定义
ostream操作符,您就不能以这种方式打印链接列表。”这意味着这里定义的链接列表。我没有说“链接列表”;)编辑:将“the”改为“your”,这可能更好。谢谢。
// LList.cpp
#include "LList.h"

LList::LList()
{
    head_ = 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;
    }
    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;
    }
    else {
        head_ = newNode;
    }
    size_ += 1;
}

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

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

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)
{
    if (this != &source) {
        dealloc();
        copy(source);
    }
    return *this;
}

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

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

    node = head_;
    while (node) {
        dnode = node;
        node = node->link_;
        delete dnode;
    }
}
#include "LList.h"
#include <iostream>
using namespace std;

int main()
{
    LList b, c;
    int x;

    b.append(1);
    b.append(2);
    b.append(3);
    c.append(4);
    c.append(5);
    c = b;
    x = b.pop();

    cout << c;
} 
Error   1   error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'LList' (or there is no acceptable conversion)    c:\users\koopt_000\documents\visual studio 2013\projects\lab10\lab10\testlist.cpp   18  1   Lab10
void LList::printList()
{ 
    ListNode *tmp = head_;

    while(tmp) {
        std::cout<<tmp->item_;
        tmp=tmp->link_;
    }      
}
...
c.printList();
....
friend std::ostream& operator<< (std::ostream& stream, const LList& list) {
    // Write code here to iterate through the list
    // and output each item...

    // Return the stream so we can chain outputs..
    return stream;
}