开发者

C++ Problem: "Unresolved external 'UberList<int>::Iter::Iter()' referenced from C:\USERS\HOME\CPPTEST\UBERLIST.OBJ"

开发者 https://www.devze.com 2023-01-31 06:29 出处:网络
I\'m trying to implement doubly-linked list in c++ and right now I\'m adding iterator features. Everything compiles without errors and (probably) goes fine until I add these methods:

I'm trying to implement doubly-linked list in c++ and right now I'm adding iterator features. Everything compiles without errors and (probably) goes fine until I add these methods:

template<class T>
typename UberList<T>::Iter UberList<T>::begin() const
{
    Iter it;
    it.curr = head;
    return it;
}


template<class T>
typename UberList<T>::Iter UberList<T>::end() const
{
    Iter it;
    it.curr = tail;
    return it;
}

and then I get this:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
UberList2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external 'UberList<int>::Iter::Iter()' referenced from C:\USERS\HOME\CPPTEST\UBERLIST2.OBJ

I saw here similar questions that it may be a linking problem, but I'm not sure if that's my case because I use only one file. I'd appreciate any help about this weird problem.

here's the full code of my class:

#include <iostream>

using namespace std;

template<class T>
class UberList
{
    friend class Iter;

    private:


    struct Node
    {
            friend class Iter;
            T data;

            Node *prev;
            Node *next;

            Node()
                    :data(0), prev(0), next(0)
            {
            }

            Node(const T& d, Node *p, Node *n)
                    : data(d), prev(p), next(n)
            {
            }

    };

    public:
    class Empty
    {
    };
    class BadIter
    {
    };

    class Iter
    {
            friend class UberList;

            public:

            //constructors
            Iter();
            Iter(Node *n开发者_开发知识库)
            {
                    curr = n;
            }


            T& operator*()
            {
                    return curr->data;
            }


            Iter& operator++()
            {
                    if(curr == 0)
                            throw BadIter();
                    curr = curr->next;
                    return *this;
            }

            Iter& operator--()
            {
                    if(curr == 0)
                            throw BadIter();
                    curr = curr->prev;
                    return *this;
            }


            bool operator==(const Iter& it)
            {
                    return curr == it.curr;
            }
            bool operator!=(const Iter& it)
            {
                    return curr != it.curr;
            }


            private:
            Node *curr;
    };


    UberList()
            :length(0), head(0), tail(0)
    {
    }
    ~UberList();


    int size()
    {
            return length;
    }

    bool empty()
    {
            return length == 0;
    }

    void pushBack(const T& elem);
    void popBack();
    void pushFront(const T& elem);
    void popFront();

    Iter begin() const;
    Iter end() const;

    Iter erase(typename UberList<T>::Iter curr);
    void insertBefore(const T& elem, Iter curr);
    private:

    int length;
    Node *head;
    Node *tail;
};
template<class T>
typename UberList<T>::Iter UberList<T>::begin() const
{
    Iter it;
    it.curr = head;
    return it;
}

template<class T>
typename UberList<T>::Iter UberList<T>::end() const
{
    Iter it;
    it.curr = tail;
    return it;
}



template<class T>
UberList<T>::~UberList()
{
    while(head != 0)
    {
            popBack();
    }
}


template<class T>
void UberList<T>::pushBack(const T& elem)
{
    if(empty())
    {

            head = tail = new Node(elem, 0, 0);
    }
    else
    {

            tail = tail -> next = new Node(elem, tail, 0);
    }
    ++length;


}


template<class T>
void UberList<T>::popBack()
{

    if(empty())
            throw Empty();


    if(length == 1)
    {
            delete head;
            head = tail = 0;

    }
    else
    {

            tail = tail->prev;

            delete tail->next;

            tail->next = 0;

    }
}

template<class T>
typename UberList<T>::Iter UberList<T>::erase(typename UberList<T>::Iter curr)
{
    if(curr.curr == 0)
            throw BadIter();
    if(curr.curr == head)
    {
            popFront();
    }
    else if(curr.curr == tail)
    {
            popBack();
    }
    else
    {

            curr.curr->prev->next = curr.curr->next;

            curr.curr->next->prev = curr.curr->prev;

            Node *t = curr.curr->next;

            delete curr.curr;
            return Iter(t);

    }


}

template<class T>
void UberList<T>::insertBefore(const T& elem, Iter curr)
{
        if(curr.curr == 0)
                throw BadIter();
        if(curr.curr == head)
        {
                pushfront(elem);
        }
        else
        {

                curr.curr->prev = curr.curr->prev->next = new Node(elem, curr.curr->prev, curr.curr);
        }
}

int main()
{

        int a;
        UberList<int> ulist;
        while (cin >> a)
        {
                ulist.pushBack(a);
        }
        for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
        {
                cout << *it << " ";
        }

        return 0;
}


The Iter default constructor is declared but not defined. Change line 48 to

Iter() { curr = NULL; }

0

精彩评论

暂无评论...
验证码 换一张
取 消