Chebz
Chebz

Reputation: 1405

C++ Iterator: no appropriate default constructor available

I am trying to implement Linked List with custom iterator. I am getting a bunch of errors trying to implement copy constructor:

'LinkedListIterator>' : no appropriate default constructor available 'LinkedListIterator> LinkedList::begin(void)' : cannot convert 'this' pointer rom 'const LinkedList' to 'LinkedList &' 'LinkedListIterator> LinkedList::end(void)' : cannot convert 'this' pointer from 'const LinkedList' to 'LinkedList &'

LinkedList

class LinkedList
{
std::unique_ptr<node> head;
std::unique_ptr<node> tail;

LinkedList(const LinkedList& other)
{
    init();
    iterator i = other.begin();
    while (i != other.end())
        add(*i++);

    head = other.head;
    tail = other.tail;
}

iterator begin() 
{
    return iterator(head->next);
}

iterator end()
{
    return iterator(tail);
}

Iterator

template <typename TNode>
class LinkedListIterator
{
    friend class LinkedList<typename TNode::value_type>;
    TNode* p;
public:
    LinkedListIterator(TNode* p) : p(p) {}
    LinkedListIterator(const LinkedListIterator& other) : p(other.p) {}
    LinkedListIterator& operator=(LinkedListIterator other) { std::swap(p, other.p); return *this; }
    void operator++() { p = p->next; }
    void operator++(int) { p = p->next; }
    bool operator==(const LinkedListIterator& other) { return p == other.p; }
    bool operator!=(const LinkedListIterator& other) { return p != other.p; }
    int& operator*() { return p->data; }
    LinkedListIterator<TNode> operator+(int i)
    {
        LinkedListIterator<TNode> iter = *this;
        while (i-- > 0 && iter.p)
        {
            ++iter;
        }
        return iter;
    }
};
}

Let me know if you need me to post more code. Thanks.

Upvotes: 0

Views: 847

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

Member functions begin() and end()are defined as non-constant member functions

iterator begin() 
{
    return iterator(head->next);
}

iterator end()
{
    return iterator(tail);
}

However you call them for const object other

LinkedList(const LinkedList& other)
{
    init();
    iterator i = other.begin(); // <== here
    while (i != other.end()) // <== here
        add(*i++);

    head = other.head;
    tail = other.tail;
}

As for error mesdsage

no appropriate default constructor available

then I do not see where the default constructor is used. Nevertheless the error message is clear enough: class LinkedListIterator<Node<T>> has no the default constructor but some where in the code you create an object of this type using the default constructor.

Upvotes: 3

karmasponge
karmasponge

Reputation: 1377

Assuming that 'iterator' is defined as 'LinkedListIterator' it appears that you are attempting to pass the 'head' and 'tail' variables (which appear to be global?) to constructors that do not exist and the compiler is making a last ditched effort to match against a copy constructor (for which there is no match).

I would assume that code should be as follows:

iterator begin() 
{
    return iterator(head->next.get());
}

iterator end()
{
    return iterator(tail.get());
}

Upvotes: 0

Related Questions