自创容器存在问题(缺少&#39 ;;'之前'<')

时间:2017-08-02 19:20:12

标签: c++ stl

我正在尝试为它创建STL容器和迭代器。我收到了编译错误:

" C2143 Missing ';' before '<' "
" C2238 Unexpected token(s) preceding ';' " 

指向我的begin()方法

typename MyIterator<data_type> begin();

我为了这个错误去了MSDN,但我仍然不明白我做错了什么。

以下是完整代码:

MyList.h:

#pragma once

#include <iostream>

namespace lab

{
    //--------------------------------
    // SUPPORTING STRUCT FOR CONTAINER
    //--------------------------------
    template <class data_type> struct _list

    {
        data_type data;
        struct _list<data_type> * next;
        struct _list<data_type> * previous;
    };


    //----------------
    // CONTAINER CLASS
    //----------------
    template <class data_type> class List

    {

    private:

        _list<data_type> * head;
        _list<data_type> * tail;
        size_t list_size;

    public:

        List();
        ~List();

        typename MyIterator<data_type> begin();

        void push_back(data_type data);
        size_t size();
    };


    template <class data_type> List<data_type>::List()

    {
        head = tail = NULL;
        list_size = NULL;
    }

    template <class data_type> List<data_type>::~List()

    {
        _list<data_type> * cur = this->head;
        _list<data_type> * tmp = NULL;

        while (cur)

        {
            tmp = cur->next;
            delete cur;
            cur = tmp;
        }
    }

    template <class data_type> size_t List<data_type>::size() 

    { 
        return list_size; 
    }

    template <class data_type> void List<data_type>::push_back(data_type data)

    {
        if (!tail)
        {
            head = new _list<data_type>;
            head->data = data;
            head->next = NULL;
            head->previous = NULL;
            tail = head;
        }
        else
        {
            _list<data_type> * temp = new _list<data_type>;
            temp->data = data;
            temp->previous = tail->next;
            temp->next = NULL;
            tail = temp;
        }
        this->list_size++;
    }

    // --------------------
    // ITERATOR CLASS
    // --------------------

    template<class data_type> class MyIterator : public std::iterator<std::input_iterator_tag, data_type>

    {
        friend class List<data_type>;

    private:

        _list<data_type> * p;

    public:

        MyIterator(const MyIterator &it);
        MyIterator(data_type * p);
        bool operator != (MyIterator const & other) const;
        bool operator == (MyIterator const & other) const; 
        typename MyIterator::reference operator * () const;
        MyIterator& operator ++ ();
    };

    template<typename data_type> MyIterator<data_type>::MyIterator(data_type * p) : p(p) {  }

    template<typename data_type> MyIterator<data_type>::MyIterator(const MyIterator & it) : p(it.p) { }

    template<typename data_type> bool MyIterator<data_type>::operator != (MyIterator const & other) const

    {
        return p->data != other.p->data;
    }

    template<typename data_type> bool MyIterator<data_type>::operator == (MyIterator const & other) const

    {
        return p->data == other.p->data;
    }

    template<typename data_type> typename MyIterator<data_type>::reference MyIterator<data_type>::operator * () const

    {
        return p->data;
    }

    template<typename data_type> MyIterator<data_type> & MyIterator<data_type>::operator ++ ()

    {
        p = p->next;
        return *this;
    }
}

我有一个名为Source.cpp的文件用于测试,但它只有一个空的main()函数。

0 个答案:

没有答案