C++;自身迭代器 我的C++代码有点问题。我有一个链表(下图),我需要为我自己的(学校作业)制作一个迭代器

C++;自身迭代器 我的C++代码有点问题。我有一个链表(下图),我需要为我自己的(学校作业)制作一个迭代器,c++,linked-list,iterator,C++,Linked List,Iterator,我在列表中有head、last和实际节点的变量 我的类迭代器是 class iterator { Node* _node; public: iterator(Node* node) : _node(node){} ~iterator(){ _node = nullptr; } iterator& operator=(const iterator& other) { _node = other._node;

我在列表中有head、last和实际节点的变量

我的类迭代器是

class iterator
{
    Node* _node;
public:
    iterator(Node* node) : _node(node){}
    ~iterator(){ _node = nullptr; }

    iterator& operator=(const iterator& other)
    {
        _node = other._node;
        return *this;
    }
    bool operator==(const iterator& other)
    {
        if (_node == nullptr || other._node == nullptr)
        {
            return false;
        }
        else
        {
            return _node->_data == other._node->_data;
        }
    }
    bool operator!=(const iterator& other)
    {
        if (_node == nullptr || other._node == nullptr)
        {
            return false;
        }
        else
        {
            return _node->_data != other._node->_data;
        }
    }

    iterator& operator++() // prefix
    {
        if (_node != nullptr)
        {
            _node = _node->_next;
        }
        return *this;
    }
    iterator operator++(int) // postfix
    {
        iterator temp(*this);
        ++(*this);
        return temp;
    }
    T& operator*() // dereference
    {
        return _node->_data;
    }
    T* operator->() // šipková notace
    {
        return &*(List<T>::iterator)*this;
    }
};
有人能帮我怎么做这两种方法吗

对不起,我的英语不好,我知道

谢谢你的帮助

编辑:

我的节点类是这个

class Node
{
public: 
    T _data;
    Node* _next;
};
我用这个循环来测试

for (List<int>::iterator it = list->begin(); it != list->end(); it++)
{
    std::cout << *it << std::endl;
}
for(List::iterator it=List->begin();it!=List->end();it++)
{

std::cout结束迭代器应该指向“过去的结束”元素,而不是实际的最后一个元素。因此,您应该真正拥有:

iterator end()
{
    return iterator(nullptr);
}
然后将
运算符==
实现为:

bool operator==(const iterator& other) { return _node == other._node; }
bool operator!=(const iterator& other) { !((*this) == other); }

为了让它接受
nullptr

,我找到了这种方法,但有了这种方法,我可能需要修改迭代器运算符,因为它没有显示任何想法。@jattanorar“它没有显示任何东西”是什么意思?如果我使用测试功能,它不会打印列表中的数据。。如果我使用您的实现运算符,它是循环的。@JattanRadar请粘贴您的整个代码(包括
main
),并在此处共享指向它的链接。
bool operator==(const iterator& other) { return _node == other._node; }
bool operator!=(const iterator& other) { !((*this) == other); }