C2676 二进制“++”:SetIterator<TElement> 未定义此运算符

Pet*_*and -1 c++ operator-overloading

我试图operator ++在我的 Set 迭代器上定义来调用方法next(),所以它会增加迭代器中的位置。

template<typename TElement>
class SetIterator {

private:

    Set<TElement>& set;
    int poz;

public:

    SetIterator(Set<TElement>& set, int poz) : set{ set }, poz{ poz } {
        while (set.elems[this->poz] == EMPTY || set.elems[this->poz] == DELETED)
            this->poz++;
    };

    SetIterator(const SetIterator& other) = default;

    ~SetIterator() = default;

    bool valid() {
        return poz < set.capacity;
    };

    void next() {
        poz++;
        while (set.elems[poz] == EMPTY || set.elems[poz] == DELETED)
            poz++;
    };

    SetIterator<TElement>& operator ++ () {
        next();
        return *this;
    };

    TElement& element() {
        return set.elems[poz];
    };
};
Run Code Online (Sandbox Code Playgroud)

问题是当我使用它时它无法识别我对运算符++的定义:

SetIterator<int> it = set.begin();// set.begin() returns an iterator with poz=0
    while (it.valid()) {
        std::cout << it.element() << std::endl;
        it++;
    };
Run Code Online (Sandbox Code Playgroud)

Igo*_* R. 5

您定义的运算符是前缀 operator,因此您应该像这样调用它:

++it;
Run Code Online (Sandbox Code Playgroud)

如果您需要后缀之一,请按如下方式声明:

SetIterator<TElement> operator ++ (int);
Run Code Online (Sandbox Code Playgroud)