c++ - 实现自定义迭代器以与 std::find 一起工作

标签 c++ c++11

我正在尝试实现一个与 std::find 和其他算法一起工作的自定义双向链表,我为它制作了一个迭代器,它与 for 循环一起工作,但是它不起作用并且给我带来了很多错误,你认为我的问题在哪里?

这是我的简化代码:

template<typename T, class Allocator = std::allocator<T>>
class MyList {
private:

    class Link {
    public:
        T item;
        std::shared_ptr<Link> next; // next item
        std::shared_ptr<Link> prev; // previous item

    };


    std::shared_ptr<Link> head;
    std::shared_ptr<Link> tail;

public:

    using allocator_type = Allocator;
    using reference_link = T &;

    class iterator {

    public:
        typedef iterator self_type;
        typedef Link value_type;
        typedef Link &reference;
        typedef std::shared_ptr<Link> pointer;
        typedef std::forward_iterator_tag iterator_category;
        typedef int difference_type;

        iterator(pointer ptr) : _link(ptr) { }

        self_type operator++() {
            std::shared_ptr<Link> temp{new Link};
            if (_link->next) {
                temp->item = _link->next->item;
                temp->next = _link->next->next;
                temp->prev = _link->next->prev;
            }
            else {
                temp->next = nullptr;
                temp->prev = _link->prev->next;
            }
            this->_link = temp;

            return *this;

        }

        self_type operator++(int junk) {

            std::shared_ptr<Link> temp{new Link};
            if (_link->next) {
                temp->item = _link->next->item;
                temp->next = _link->next->next;
                temp->prev = _link->next->prev;
            }
            else {
                temp->next = nullptr;
                temp->prev = _link->prev->next;
            }
            this->_link = temp;

            return *this;
        }

        reference operator*() { return *_link; }

        pointer operator->() { return _link; }

        bool operator==(const self_type &l) const {

            if (_link->next == l._link->next && _link->prev == l._link->prev && _link->item == l._link->item) {
                return true;
            }
            return false;
        }

        bool operator!=(const self_type &l) const {
            return !operator==(l);
        }

        friend bool operator==(const iterator &lhs, const T &rhs) {
            if (lhs->item == rhs) {
                return true;
            }
            return false;
        };


    private:
        pointer _link;
    };


    iterator begin();

    iterator end();

    using iterator = iterator;
    using const_iterator = const iterator;

};

template<typename T, class Allocator>
typename MyList<T, Allocator>::iterator MyList<T, Allocator>::begin() {

    return iterator{tail};
}


template<typename T, class Allocator>
typename MyList<T, Allocator>::iterator MyList<T, Allocator>::end() {

    return (iterator{head})++;
}

这个很好用:

MyList<int, std::allocator<int>> m_list{1, 2, 3,};
for (MyList<int, std::allocator<int>>::iterator i = m_list.begin();
 i != m_list.end(); ++i) {
                std::cout << i->item;
}

但这给了我很多错误:

auto result1 = std::find(m_list.begin(), m_list.end(),  3 );

有哪些:

In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of ‘_InputIterator std::__find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = MyList<int, std::allocator<int> >::iterator; _Tp = int]’:
/usr/include/c++/4.8/bits/stl_algo.h:4441:45:   required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = MyList<int, std::allocator<int> >::iterator; _Tp = int]’
/home/epezhman/cpp/Part1/assignment4/src/main.cpp:44:63:   required from here
/usr/include/c++/4.8/bits/stl_algo.h:139:46: error: no match for ‘operator==’ (operand types are ‘MyList<int, std::allocator<int> >::Link’ and ‘const int’)
       while (__first != __last && !(*__first == __val))
                                              ^
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note: candidates are:
In file included from /usr/include/c++/4.8/random:52:0,
                 from /usr/include/c++/4.8/bits/stl_algo.h:65,
                 from /usr/include/c++/4.8/algorithm:62,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/random.tcc:2030:5: note: template<class _RealType1> bool std::operator==(const std::normal_distribution<_RealType>&, const std::normal_distribution<_RealType>&)
     operator==(const std::normal_distribution<_RealType>& __d1,
     ^
/usr/include/c++/4.8/bits/random.tcc:2030:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::normal_distribution<_RealType>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/vector:64:0,
                 from /usr/include/c++/4.8/bits/random.h:34,
                 from /usr/include/c++/4.8/random:50,
                 from /usr/include/c++/4.8/bits/stl_algo.h:65,
                 from /usr/include/c++/4.8/algorithm:62,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_vector.h:1404:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
     operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     ^
/usr/include/c++/4.8/bits/stl_vector.h:1404:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::vector<_Tp, _Alloc>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/iterator:66:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:4,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stream_iterator.h:130:5: note: template<class _Tp, class _CharT, class _Traits, class _Dist> bool std::operator==(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&, const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&)
     operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
     ^
/usr/include/c++/4.8/bits/stream_iterator.h:130:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/bits/locale_facets.h:48:0,
                 from /usr/include/c++/4.8/bits/basic_ios.h:37,
                 from /usr/include/c++/4.8/ios:44,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:2,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
     ^
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:82:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/shared_ptr.h:338:5: note: template<class _Tp> bool std::operator==(std::nullptr_t, const std::shared_ptr<_Tp1>&)
     operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
     ^
/usr/include/c++/4.8/bits/shared_ptr.h:338:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   cannot convert ‘__first.MyList<T, Allocator>::iterator::operator*<int, std::allocator<int> >()’ (type ‘MyList<int, std::allocator<int> >::Link’) to type ‘std::nullptr_t’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:82:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/shared_ptr.h:333:5: note: template<class _Tp> bool std::operator==(const std::shared_ptr<_Tp1>&, std::nullptr_t)
     operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
     ^
/usr/include/c++/4.8/bits/shared_ptr.h:333:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::shared_ptr<_Tp1>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:82:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/shared_ptr.h:327:5: note: template<class _Tp1, class _Tp2> bool std::operator==(const std::shared_ptr<_Tp1>&, const std::shared_ptr<_Tp2>&)
     operator==(const shared_ptr<_Tp1>& __a,
     ^
/usr/include/c++/4.8/bits/shared_ptr.h:327:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::shared_ptr<_Tp1>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/bits/shared_ptr.h:52:0,
                 from /usr/include/c++/4.8/memory:82,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/shared_ptr_base.h:1040:5: note: template<class _Tp, __gnu_cxx::_Lock_policy _Lp> bool std::operator==(std::nullptr_t, const std::__shared_ptr<_Tp, _Lp>&)
     operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
     ^
/usr/include/c++/4.8/bits/shared_ptr_base.h:1040:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   cannot convert ‘__first.MyList<T, Allocator>::iterator::operator*<int, std::allocator<int> >()’ (type ‘MyList<int, std::allocator<int> >::Link’) to type ‘std::nullptr_t’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/bits/shared_ptr.h:52:0,
                 from /usr/include/c++/4.8/memory:82,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/shared_ptr_base.h:1035:5: note: template<class _Tp, __gnu_cxx::_Lock_policy _Lp> bool std::operator==(const std::__shared_ptr<_Tp, _Lp>&, std::nullptr_t)
     operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
     ^
/usr/include/c++/4.8/bits/shared_ptr_base.h:1035:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::__shared_ptr<_Tp, _Lp>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/bits/shared_ptr.h:52:0,
                 from /usr/include/c++/4.8/memory:82,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/shared_ptr_base.h:1029:5: note: template<class _Tp1, class _Tp2, __gnu_cxx::_Lock_policy _Lp> bool std::operator==(const std::__shared_ptr<_Tp1, _Lp>&, const std::__shared_ptr<_Tp2, _Lp>&)
     operator==(const __shared_ptr<_Tp1, _Lp>& __a,
     ^
/usr/include/c++/4.8/bits/shared_ptr_base.h:1029:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::__shared_ptr<_Tp1, _Lp>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:81:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:500:5: note: template<class _Tp, class _Dp> bool std::operator==(std::nullptr_t, const std::unique_ptr<_Tp, _Dp>&)
     operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
     ^
/usr/include/c++/4.8/bits/unique_ptr.h:500:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   cannot convert ‘__first.MyList<T, Allocator>::iterator::operator*<int, std::allocator<int> >()’ (type ‘MyList<int, std::allocator<int> >::Link’) to type ‘std::nullptr_t’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:81:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:495:5: note: template<class _Tp, class _Dp> bool std::operator==(const std::unique_ptr<_Tp, _Dp>&, std::nullptr_t)
     operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
     ^
/usr/include/c++/4.8/bits/unique_ptr.h:495:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:81:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:489:5: note: template<class _Tp, class _Dp, class _Up, class _Ep> bool std::operator==(const std::unique_ptr<_Tp, _Dp>&, const std::unique_ptr<_Up, _Ep>&)
     operator==(const unique_ptr<_Tp, _Dp>& __x,
     ^
/usr/include/c++/4.8/bits/unique_ptr.h:489:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:79:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/functional:2543:5: note: template<class _Res, class ... _Args> bool std::operator==(std::nullptr_t, const std::function<_Res(_ArgTypes ...)>&)
     operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
     ^
/usr/include/c++/4.8/functional:2543:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   cannot convert ‘__first.MyList<T, Allocator>::iterator::operator*<int, std::allocator<int> >()’ (type ‘MyList<int, std::allocator<int> >::Link’) to type ‘std::nullptr_t’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/memory:79:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/functional:2537:5: note: template<class _Res, class ... _Args> bool std::operator==(const std::function<_Res(_ArgTypes ...)>&, std::nullptr_t)
     operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
     ^
/usr/include/c++/4.8/functional:2537:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::function<_Res(_ArgTypes ...)>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/functional:55:0,
                 from /usr/include/c++/4.8/memory:79,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/tuple:813:5: note: template<class ... _TElements, class ... _UElements> constexpr bool std::operator==(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&)
     operator==(const tuple<_TElements...>& __t,
     ^
/usr/include/c++/4.8/tuple:813:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::tuple<_Elements ...>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/tuple:39:0,
                 from /usr/include/c++/4.8/functional:55,
                 from /usr/include/c++/4.8/memory:79,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/array:228:5: note: template<class _Tp, long unsigned int _Nm> bool std::operator==(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)
     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
     ^
/usr/include/c++/4.8/array:228:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::array<_Tp, _Nm>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/stdexcept:39,
                 from /usr/include/c++/4.8/array:38,
                 from /usr/include/c++/4.8/tuple:39,
                 from /usr/include/c++/4.8/functional:55,
                 from /usr/include/c++/4.8/memory:79,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/stdexcept:39,
                 from /usr/include/c++/4.8/array:38,
                 from /usr/include/c++/4.8/tuple:39,
                 from /usr/include/c++/4.8/functional:55,
                 from /usr/include/c++/4.8/memory:79,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator==(const _CharT* __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   mismatched types ‘const _CharT*’ and ‘MyList<int, std::allocator<int> >::Link’
       while (__first != __last && !(*__first == __val))
                                              ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/stdexcept:39,
                 from /usr/include/c++/4.8/array:38,
                 from /usr/include/c++/4.8/tuple:39,
                 from /usr/include/c++/4.8/functional:55,
                 from /usr/include/c++/4.8/memory:79,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:1,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
     operator==(const basic_string<_CharT>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from /home/epezhman/cpp/Part1/assignment4/exercise_1/include/my_list.h:5,
                 from /home/epezhman/cpp/Part1/assignment4/src/main.cpp:1:
/usr/include/c++/4.8/bits/stl_algo.h:139:46: note:   ‘MyList<int, std::allocator<int> >::Link’ is not derived from ‘const std::basic_string<_CharT>’
       while (__first != __last && !(*__first == __val))

最佳答案

看起来您的value_typereference 应该使用T 而不是Link。 (您想遍历这些值,而不是包含它们的链接。)operator *operator -> 需要适当更改。

关于c++ - 实现自定义迭代器以与 std::find 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33708918/

相关文章:

c++ - 如何使用 OpenCV::ML 管理 ANN 内部神经元的连接?

c++ - LoadLibrary貌似加载错误的DLL

c++ - 使用 std::move 实现 "take"方法

g++ - C++0x 标准中是否有任何内容支持模板的单独编译?

c++ - 模板函数特化默认

c++ - 使用多个着色器的最佳方式

c++ - C++:在SIGUSR信号上序列化对象

c++ - clang6 实现 std::可选 吗?

c++ - 两种模板类型的 sizeof,当两者都派生自一个基类时

c++ - std::map 从多个线程写入/读取