0

I have the following code, used as a part of a Linked List:

// copy constructor:
LinkedList<T>(const LinkedList<T> &list) 
{
    // make a deep copy
    for (LinkedList<T>::Iterator i = list.begin(); i != list.end(); i++)
    {
        add(*i);
    }
}


// assignment constructor
LinkedList<T>& operator= (const LinkedList<T> &list) 
{
    // make a deep copy
    for (LinkedList<T>::Iterator i = list.begin(); i != list.end(); i++)
    {
        add(*i);
    }
}

But when I compile I get the following errors (this is when I use it as an assignment constructor):

1>------ Build started: Project: AnotherLinkedList, Configuration: Debug Win32 ------
1>main.cpp
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): error C2662: 'LinkedList<int>::Iterator LinkedList<int>::begin(void)': cannot convert 'this' pointer from 'const LinkedList<int>' to 'LinkedList<int> &'
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): note: Conversion loses qualifiers
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(55): note: while compiling class template member function 'LinkedList<int> &LinkedList<int>::operator =(const LinkedList<int> &)'
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\main.cpp(20): note: see reference to function template instantiation 'LinkedList<int> &LinkedList<int>::operator =(const LinkedList<int> &)' being compiled
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\main.cpp(14): note: see reference to class template instantiation 'LinkedList<int>' being compiled
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): error C2662: 'LinkedList<int>::Iterator LinkedList<int>::end(void)': cannot convert 'this' pointer from 'const LinkedList<int>' to 'LinkedList<int> &'
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): note: Conversion loses qualifiers
1>Done building project "AnotherLinkedList.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The iterator code for begin and end looks like this:

// get root
    Iterator begin()
    {
        return Iterator(sp_Head);
    }

    // get end
    Iterator end()
    {
        return Iterator(nullptr);
    }

What do I do wrong?

7
  • FYI -- Your assignment operator does not remove the old list and fails to return a value. Commented Nov 19, 2019 at 20:15
  • 1
    True, but that's not the reason for the compilation error. The compilation error is because begin/end are not const class methods, and they must be. Free karma to whoever wants it... Commented Nov 19, 2019 at 20:17
  • @PaulMcKenzie: i see your point. of course i need to delete all the existing items in the list, add the new one, and return *this (correct?) Commented Nov 19, 2019 at 20:28
  • You could do that, but using copy / swap is better. Commented Nov 19, 2019 at 20:31
  • Iterator(nullptr) <-- this looks like a problem waiting to happen. Will the iterator always point to a nullptr at the end? Commented Nov 19, 2019 at 20:34

1 Answer 1

3

Based on the error message, it would seem that your LinkedList does not have variants of begin() and end() that can be called on a const object. The parameters to your copy constructor and assignment operator are const, however. You will have to add const versions of begin() and end().

Presumably, you're looking for something like this:

    ConstIterator begin() const { Iterator(sp_Head); }
    Iterator begin() { Iterator(sp_Head); }

    ConstIterator end() const { ConstIterator(nullptr); }
    Iterator end() { Iterator(nullptr); }

Where ConstIterator is a version of your iterator type that iterates over const elements…

Sign up to request clarification or add additional context in comments.

6 Comments

Should i add methods that returns const ? I have tried to add change them to const Iterator, but with the same result
@tannic read the error "Conversion loses qualifiers" the member function has to be const qualified.
@tannic If you have a const list, then that usually means that the content of the list is to be logically const, i.e., someone with a const LinkedList<T>& should not be able to modify the content of the list. If you return a const iterator, that just means the iterator itself cannot be modified. What you usually want is for begin and end on a const list to return iterators that iterate over const T rather than T
@MichaelKenzel so need to make a ConstIterator class, that should (allmost) be a copy of my current Iterator ?
@tannic I tend to make the iterator a class template, template<typename Type> class iterator_impl { using value_type = Type; ... }; and after the iterator class template definition just do using iterator = iterator_impl<T>; using const_iterator = iterator_impl<T const>; to get both.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.