2014-09-28 58 views
2

我正在使用C++來實現LinkedList,其他函數和運算符我可以創建Node *就好了。然而,當我得到這個操作「的ostream &操作< <(ostream的&出來,常量LinkedList的&列表)」(輸出列表),我不能創建一個臨時運營商裏面,誰能告訴我是什麼原因造成的錯誤以及如何解決它?C++:無法在ostream&operator中創建節點指針<<

這裏是我的LinkedList.h

#ifndef LINKEDLIST_H 
#define LINKEDLIST_H 

#include <iostream> 

using namespace std; 

class LinkedList { 
    typedef struct Node{ 
     int data; 
     Node* next; 

    bool operator < (const Node& node) const { 
     return this->data < node.data; 
    } 

    bool operator <= (const Node& node) const { 
     return this->data <= node.data; 
    } 

    bool operator > (const Node& node) const { 
     return this->data > node.data; 
    } 

    bool operator >= (const Node& node) const { 
     return this->data >= node.data; 
    } 

    friend ostream& operator << (ostream& out, const LinkedList& list); 
    friend istream& operator >> (istream& in, const LinkedList& list); 

    } * nodePtr; 

public: 
    nodePtr head; 
    nodePtr curr; 
    LinkedList(); 

    // functions 
    void push_front(int); 
    void push_back(int); 
    int pop_front(); 
    int pop_back(); 
    int size(); 
    bool contains(int); 
    void print(); 
    void clear(); 

    // overload 
    LinkedList& operator =(const LinkedList& list); 
    bool operator !=(const LinkedList& list) const; 
    LinkedList operator +(const int v) const; 
    LinkedList operator +(const LinkedList& list) const; 
    LinkedList operator - (const int v) const; 
    friend ostream& operator << (ostream& out, const LinkedList& list); 
    friend istream& operator >> (istream& in, const LinkedList& list); 
    }; 

#endif /* LINKEDLIST_H */ 
在我LinkedList.cpp

ostream& operator << (ostream& out, const LinkedList& list) { 
    nodePtr temp = list.head;   <----------------- **Unable to resolve identifier nodePtr** 
} 

我可以創造我的其他功能就好節點*(NODEPTR)。

+0

您的其他功能實際上LinkedList'的'的一部分。這一個不是。名稱查找規則的改變基於同樣的原因,您不希望'main'能夠執行'nodePtr temp'並讓它找到您的類。 – chris 2014-09-28 02:45:46

+0

我能做些什麼來解決它? – Jnk 2014-09-28 02:51:29

回答

2

nodePtr是在LinkedList裏面定義的,它需要合格。更改

nodePtr temp = list.head; 

LinkedList::nodePtr temp = list.head; 
相關問題