我正在尝试建立一个我在网上找到的项目.出于某种原因,即使看起来我不应该,我也会收到错误.它有以下几类:
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;
};
Run Code Online (Sandbox Code Playgroud)
但是,当它在项目中使用时,我收到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();
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?有人可以解释并提供解决方案吗?
你正在重载错误的运营商.
operator ++ ()超载++foo 不是 foo++.
要么改变你的for循环,要么重载operator ++ (int).int这与您的数据类型无关:它只是区分两个运营商版本的一种方式.
最后,您的返回类型应为smit&for operator++()和smitfor operator ++ (int).