我正在尝试建立一个我在网上找到的项目。由于某些原因,我得到了错误,尽管它看起来不应该。它有以下类:
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();
}为什么会发生这种情况?有没有人能解释一下,或许能提供一个解决方案?
发布于 2015-09-24 18:44:33
你重载了错误的运算符。
operator ++ ()重载++foo 而不是 foo++。
要么更改您的for循环,要么重载operator ++ (int)。这里的int与您的数据类型无关:它只是区分两个运算符版本的一种方法。
最后,对于operator++(),您的返回类型应该是smit&,对于operator ++ (int),您的返回类型应该是smit。
https://stackoverflow.com/questions/32759247
复制相似问题