如何修复错误C2676:具有抽象数据类型的链表迭代器的二进制“ ++”

时间:2019-04-27 16:55:52

标签: c++ linked-list operator-overloading listiterator

我正在尝试使用迭代器在双链表上进行气泡排序。但是,当我尝试使用已超载的“ ++”运算符时,出现以下错误:

source.cpp(93): error C2676: binary '++': 'linkedListIterator<Type>' does not define this operator or a conversion to a type acceptable to the predefined operator
1>        with
1>        [
1>            Type=Person
1>        ]

我已经查看了所有相关帖子,但似乎找不到解决方法

Iterator头中的++操作符声明超载:


linkedListIterator<Type>& operator++();

迭代器实现中的++运算符重载:


    template<class Type>
    linkedListIterator<Type>& linkedListIterator<Type>::operator++()
    {
        if (current)
        {
            current = current->link;
            return *this;
        }
    }

DoublyLinkedList的开始和结束函数:


    template<class Type>
    linkedListIterator<Type> doublyLinkedList<Type>::begin()
    {
        return linkedListIterator<Type>(first);
    }

    template<class Type>
    linkedListIterator<Type> doublyLinkedList<Type>::end()
    {

    return linkedListIterator<Type>(last);

}

Source.cpp中的气泡排序函数,主要是:


    void bubbleSort(doublyLinkedList<Person> &list)
    {
        bool sorted = false;

        while (!sorted)
        {
            sorted = true;
            if (list.length() < 2)
            {
                return;
            }

            linkedListIterator<Person> iter = list.begin();
            linkedListIterator<Person> iter_prev = list.begin();
            iter++;

            while (iter != list.end())
            {
                if ((*iter).getAge() > (*iter_prev).getAge())
                {
                    sorted = false;
                    Person temp = (*iter);
                    (*iter) = (*iter_prev);
                    (*iter_prev) = temp;
                }
                iter++;
                iter_prev++;
            }
        }
    }

我想让冒泡排序功能遍历链表,但相反,我在第93、104和105行遇到了上面的错误。

0 个答案:

没有答案