2013-08-05 158 views
1

我想弄清楚我在做這個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*); 
}; 
+1

更換cout << *i << " ";嘗試簡單的東西:'節點n; cout << n;'。如果這不起作用,那麼你忘了定義'ostream&Node :: operator <<(ostream&)'。 – Beta

回答

0

隨着錯誤消息指出

No operator "<<" matches these operands. 

你不必爲你的類定義的< <操作。

1

你需要重載operator<<爲節點類型:

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

我已經更新了我的課的問題,我沒有List :: Iterator。 – KQball

0

在你printList()功能,具有
cout << i->getDescription() << endl;