gpt4 book ai didi

C++ C2676 错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:41 24 4
gpt4 key购买 nike

我正在尝试构建我在网上找到的项目。出于某种原因,我收到了错误,尽管看起来我不应该这样做。它有以下类:

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 .

以下是一些用法示例:

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();
}

为什么会这样?有人可以解释并提供解决方案吗?

最佳答案

您重载了错误的运算符。

operator ++ ()重载 ++foo foo++ .

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

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

关于C++ C2676 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32759247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com