实现链表[C ++]编译错误

时间:2021-07-30 07:22:15

I have the following code:

template <class T> 
class List {
    public:
        class Iterator;
        class ConstIterator;

        //Constructors and Destructors.
        List() : head(NULL), tail(NULL), size(0) {}
        List(const List& list);
        ~List();

        //Methods
        Iterator begin();
        Iterator end();
        void insert(const T& data);
        void insert(const T& data, const Iterator& iterator);
        void remove(const Iterator& iterator);
        int getSize() const;
        Iterator find();
        void sort();

        //Operators
        List operator = (const List& list);


    private:
        class Node;
        Node* head;
        Node* tail;
        int size;
};


template <class T>
class List<T>::Node
{
    public:
        //Constructors and destructors
        Node(const T& _data, const Node* _next) : data(_data), next(_next) {}       
        ~Node(); //Destructor 

        //Methods


        //Operators
        Node operator = (const Node& node);

    private:
        T data;
        Node* next;
};


template<class T> 
class List<T>::Iterator 
{
    public:
        Iterator() : list(NULL), node(NULL){} //Constructor
        Iterator(const Iterator& it) : list(it.list), node(it.node) {}
        ~Iterator(); //Destructor

        Iterator& operator=(const Iterator& it); 
        T& operator * ();
        T& operator ++ ();
        T operator ++ (int);
        T& operator -- ();
        T operator -- (int);
        bool operator == (const Iterator& iterator) const;
        bool operator != (const Iterator& iterator) const;


    private:
        List<T>* list;
        Node* node;
};


template<class T> 
class List<T>::ConstIterator 
{
    public:
        ConstIterator() : list(NULL), node(NULL){}
        ConstIterator(const ConstIterator& it) : list(it.list), node(it.node) {}
        ~ConstIterator(); //Destructor

        ConstIterator& operator=(const ConstIterator& it); 
        T& operator * ();
        T& operator ++ ();
        T operator ++ (int);
        T& operator -- ();
        T operator -- (int);
        bool operator == (const ConstIterator& iterator) const;
        bool operator != (const ConstIterator& iterator) const;


    private:
        const List<T>* list;
        const Node* node;
};

template<class T>
Iterator List<T>::begin() {
    return Iterator(this, head);
}

When I try to compile I get the following error:

当我尝试编译时,我收到以下错误:

error: expected constructor, destructor, or type conversion before ‘List’

On line:

Iterator List<T>::begin() {

I'm not sure what I'm doing wrong.

我不确定我做错了什么。

2 个解决方案

#1


4  

Iterator is not defined, but List<T>::Iterator is. You will also need to add typename:

Iterator未定义,但List :: Iterator是。您还需要添加typename:

template<class T>
typename List<T>::Iterator List<T>::begin() { ... };

Here, typename is required as an ambiguitator to tell the compiler that List<T>::Iterator is a type (rather than a static member). This is always required in the templated context (see here).

这里,typename是一个ambiguitator,要告诉编译器List :: Iterator是一个类型(而不是静态成员)。在模板化的上下文中总是需要这个(见这里)。

#2


0  

if you write the body of the function outside the class declaration, it should be:

如果你在类声明之外编写函数体,它应该是:

typename List<T>::Iterator List<T>::begin() { ... }

typename List :: Iterator List :: begin(){...}

edit: typename added

编辑:添加了typename

#1


4  

Iterator is not defined, but List<T>::Iterator is. You will also need to add typename:

Iterator未定义,但List :: Iterator是。您还需要添加typename:

template<class T>
typename List<T>::Iterator List<T>::begin() { ... };

Here, typename is required as an ambiguitator to tell the compiler that List<T>::Iterator is a type (rather than a static member). This is always required in the templated context (see here).

这里,typename是一个ambiguitator,要告诉编译器List :: Iterator是一个类型(而不是静态成员)。在模板化的上下文中总是需要这个(见这里)。

#2


0  

if you write the body of the function outside the class declaration, it should be:

如果你在类声明之外编写函数体,它应该是:

typename List<T>::Iterator List<T>::begin() { ... }

typename List :: Iterator List :: begin(){...}

edit: typename added

编辑:添加了typename