我在一个类中做了一个运算符重载函数。 我做了另一个函数,它回顾了类中的重载函数。如何在C++中使用运算符重载函数?
我想用这使人想起在主要功能的操作重载函数,所以我在主函数中写了这样的功能:我有错误C2676
#include <iostream>
...
class Zealot {
int x;
....
void operator++() {
Zealot s;
s.x = -50;
for (auto i = 0; i < 2; ++i, tail.push_back(s));
}
void Collision() {
...
(*this)++; // Error : C2676
...
}
...
};
Zealot z;
int main() {
z.Coliision();
}
所以我不能编译源代码。
我应该怎么做才好? 我需要你的大力帮助。
你定义的运算符是'++'的前缀形式,所以你可以像'++(* this);'那样调用它。后缀形式通过类型为'int'的参数来区分(所以不用),所以它是:'void operator ++(int)'。请注意,'operator ++()'通常应该返回对刚增加的对象的引用。 –
@Jerry Coffin谢谢你的帮助!我会牢记这一点。 – starrykss