首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ C2676错误

C++ C2676错误
EN

Stack Overflow用户
提问于 2015-09-24 18:38:50
回答 1查看 236关注 0票数 0

我正在尝试建立一个我在网上找到的项目。由于某些原因,我得到了错误,尽管它看起来不应该。它有以下类:

代码语言:javascript
复制
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;
};

然而,当在项目中使用它时,我得到了C2676错误:inary '++': 'util::smit<K,V>' does not define this operator or a conversion to a type acceptable to the predefined operator

下面是一些用法示例:

代码语言:javascript
复制
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();
    }

为什么会发生这种情况?有没有人能解释一下,或许能提供一个解决方案?

EN

回答 1

Stack Overflow用户

发布于 2015-09-24 18:44:33

你重载了错误的运算符。

operator ++ ()重载++foo 而不是 foo++

要么更改您的for循环,要么重载operator ++ (int)。这里的int与您的数据类型无关:它只是区分两个运算符版本的一种方法。

最后,对于operator++(),您的返回类型应该是smit&,对于operator ++ (int),您的返回类型应该是smit

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32759247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档