2012-09-06 25 views
0

嗨我試圖使用模板和ADT實現鏈接列表。目前我有兩堂課。一個是鏈接列表的迭代器,另一個是鏈接列表的基類,我將用它來派生鏈表的類。爲什麼我得到沒有類型錯誤的聲明 - 鏈表?

當試圖實現兩個功能,這將使我在開始和列表respectivly結束,我得到編譯錯誤說一個迭代「ISO C++禁止無類型‘linkedListIterator’的聲明」

這裏是對於迭代器的定義的代碼:

#ifndef LINKEDLISTITERATOR_H 
#define LINKEDLISTITERATOR_H 

#include <stddef.h> //for NULL 
#include "nodetype.h" 
#include "linkedlisttype.h" 


template <class Type> 
class linkedListIterator 
{ 
public: 
    linkedListIterator(); 

    linkedListIterator(nodeType<Type> *ptr); 

    Type operator*(); 

    linkedListIterator<Type> operator++(); 

    bool operator==(const linkedListIterator<Type>& right) const; 

    bool operator!=(const linkedListIterator<Type>& right) const; 

private: 
    nodeType<Type> *current; 
}; 

#endif // LINKEDLISTITERATOR_H 

這裏是節點類型

#ifndef NODETYPE_H_INCLUDED 
#define NODETYPE_H_INCLUDED 

//Definition of the node 
template <class Type> 
struct nodeType 
{ 
    Type info; 
    nodeType<Type> *link; 
}; 

#endif // NODETYPE_H_INCLUDED 

這裏的定義的代碼是definiti對鏈表基類:

#ifndef LINKEDLISTTYPE_H 
#define LINKEDLISTTYPE_H 

#include "nodetype.h" 
#include "linkedlistiterator.h" 

//Definition of linked list 
template <class Type> 
class linkedListType 
{ 
public: 
    const linkedListType<Type>& operator= 
    (const linkedListType<Type>&); 

    void initializeList(); 

    bool isEmptyList() const; 

    void print() const; 

    int length() const; 

    void destroyList(); 

    Type front() const; 

    Type back() const; 

    virtual bool search(const Type& searchItem) const = 0; 

    virtual void insertFirst(const Type& newItem) = 0; 

    virtual void insertLast(const Type& newItem) = 0; 

    virtual void deleteNode(const Type& deleteItem) = 0; 

    // this is where the error comes  
    linkedListIterator<Type> begin(); 

    // and here as well 
    linkedListIterator<Type> end(); 

    linkedListType(); 

    linkedListType(const linkedListType<Type>& otherList); 

    ~linkedListType(); 

protected: 
    int count; 
    nodeType<Type> *first; 
    nodeType<Type> *last; 

private: 
    void copyList(const linkedListType<Type>& otherList); 
}; 

#endif // LINKEDLISTTYPE_H 

我是新來的模板和ADT,所以試圖圍繞這一點我的思想。任何幫助將最受歡迎。

+1

您可以編輯您的問題以包含_complete_錯誤消息嗎?如果有多條消息,你應該把它放在那裏。 –

回答

1

您有兩個標題,每個標題都試圖包含對方。結果是,如果#include "linkedlistiterator.h"linkedListType的定義出現在linkedListIterator的定義之前;因此linkedListIterator的錯誤未在此處聲明。

在這種情況下,它看起來像迭代器類型根本不依賴於列表類型,因此您可以簡單地從"linkedlistiterator.h"中刪除#include "linkedlistlype.h"

+0

感謝Mike,我之前在「linkedlistlype.h」中有過nodetype定義,所以我將這個頭文件包含到「linkedlistiterator.h」中,因此它新增了一個nodetype。奇怪的是,人們會怎麼看待「必須有錯誤的新東西」,而忘記再看一遍。非常感激。 – georgelappies

1

似乎linkedlisttype.hlinkedlistiterator.h包括彼此。

這表明您的頭腦中存在相當緊密的耦合。你可能想要有LinkedList<T>類和嵌套LinkedList<T>::Iterator類。

相關問題