我想弄清楚我在做這個printList函數的錯誤。我得到以下編譯器錯誤:打印函數 - 鏈接列表C++
No operator "<<" matches these operands.
的功能如下:
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*);
};
更換
cout << *i << " ";
嘗試簡單的東西:'節點n; cout << n;'。如果這不起作用,那麼你忘了定義'ostream&Node :: operator <<(ostream&)'。 – Beta