[英]Print Function - Linked-List c++
我试图找出这个printList函数在做什么错。 我收到以下编译器错误:
没有运算符“ <<”与这些操作数匹配。
功能如下:
void printList(const List& theList)
{
for(Node* i = theList.getFirst(); i != theList.getLast(); ++i)
{
cout << *i << " ";
cout << endl;
}
}
我也有以下内容
#include "List.h"
#include <iostream>
我在想我的打印功能还差得远。 谁能指出我正确的方向?
这是我的课程,我没有List :: Iterator。 你有什么建议?
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*);
};
您需要对节点类型重载operator<<
:
std::ostream& operator<<(std:::ostream& os, const Node& node)
{
os << node.getQuantityName() << " " << node.getDescription();
return os;
}
如错误消息所述
No operator "<<" matches these operands.
您没有为您的班级定义<<操作符。
在您的printList()
函数中,替换cout << *i << " ";
同
cout << i->getDescription() << endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.