简体   繁体   English

打印功能-链表c ++

[英]Print Function - Linked-List c++

I am trying to figure out what I am doing wrong with this printList Function. 我试图找出这个printList函数在做什么错。 I am getting the following compiler error: 我收到以下编译器错误:

No operator "<<" matches these operands. 没有运算符“ <<”与这些操作数匹配。

The function is as follows: 功能如下:

void printList(const List& theList)
{   
for(Node* i = theList.getFirst(); i != theList.getLast(); ++i)
{
    cout << *i << " ";
    cout << endl;
}
}

I have the following as well, 我也有以下内容

#include "List.h"
#include <iostream>

I am thinking my print function is just way off base. 我在想我的打印功能还差得远。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Here are my classes, I don't have a List::Iterator. 这是我的课程,我没有List :: Iterator。 What would you suggest? 你有什么建议?

class List
{
private:
    int nodeListTotal;
    Node* first;
    Node* last;

public:
    //Constructor
    List();

    void push_back(Node*);
    void push_front(Node*);
    Node* pop_back();
    Node* pop_front();
    Node* getFirst() const;
    Node* getLast() const;
    int getListLength() const;
    void retrieve(int index, int& dataItem) const;
};

class Node
{
private:
    string dataItem;
    string dataUnit;
    int unitTotal;
    Node* next;

public:
    //Constructor
    Node();

    Node(int, string, string);

    string getDescription( );
    void setDescription(string);

    string getQuantityName();
    void setQuantityName(string);

    int getQuantityNumber();
    void setQuantityNumber(int);

    Node* getNext( );
    void setNext(Node*);
};

You need to overload operator<< for Node type: 您需要对节点类型重载operator<<

std::ostream& operator<<(std:::ostream& os, const Node& node)
{
    os << node.getQuantityName() << " " << node.getDescription();
    return os;
}

As the error message says 如错误消息所述

No operator "<<" matches these operands.

you don't have the << operator defined for your class. 您没有为您的班级定义<<操作符。

In your printList() function, replace cout << *i << " "; 在您的printList()函数中,替换cout << *i << " "; with
cout << i->getDescription() << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM