Gilbert Williams
Gilbert Williams

Reputation: 1050

C++ C2676 error

I'm trying to build a project I found online. For some reason, I'm getting errors even though it seems like I shouldn't. It has the following class:

template <typename K, typename V>
class smit
{
public:
    smit(map<K, V>* std, vector<K>* ky, SRWLOCK* lock, int t)
    {
        stdmap = std;
        keys = ky;
        maplock = lock;
        top = t;
        index = 0;
    }

    ~smit()
    {
        ReleaseSRWLockShared(maplock);
    }

    V* operator -> ()
    {
        return &stdmap->at(keys->at(index));
    }

    V* get()
    {
        return &stdmap->at(keys->at(index));
    }

    void operator ++ ()
    {
        index++;
    }

    bool belowtop()
    {
        return index < top;
    }

    int getindex()
    {
        return index;
    }
private:
    map<K, V>* stdmap;
    vector<K>* keys;
    SRWLOCK* maplock;
    int index;
    int top;
};

However, when it's used in the project I'm getting the C2676 error: inary '++': 'util::smit<K,V>' does not define this operator or a conversion to a type acceptable to the predefined operator.

Here are some examples of usage:

for (smit<int, mob> mbit = mobs.getit(); mbit.belowtop(); mbit++)
    {
        mbit->draw(viewpos);
    }

for (smit<int, otherplayer> plit = chars.getit(); plit.belowtop(); plit++)
    {
        plit->update();
    }

Why is it happening? Can someone explain and perhaps supply a solution?

Upvotes: 0

Views: 359

Answers (1)

Bathsheba
Bathsheba

Reputation: 234635

You're overloading the wrong operator.

operator ++ () overloads ++foo not foo++.

Either change your for loop, or overload operator ++ (int). int here has nothing to do with your data type: it's just a way of distinguishing the two operator versions.

Lastly, your return type should be smit& for operator++() and smit for operator ++ (int).

Upvotes: 5

Related Questions