我有一个Polynomial
类,我正在尝试定义operator++
前增量和后增量,以及尝试定义减量前和后减量,即operator--
。这是我的代码片段:
class Polynomial
{
public:
Polynomial();
Polynomial(vector<int>coeffs);
/*
.
.
.
*/
Polynomial operator++();
Polynomial& operator++ (int unused);
Polynomial operator--();
Polynomial& operator-- (int unused);
/*
.
.
.
*/
private:
vector<int> coefficient;
};
Polynomial Polynomial::operator++()
{
coefficient[0]++;
return *this;
}
Polynomial& Polynomial::operator++ (int unused)
{
Polynomial copy(*this);
coefficient[0]++;
return copy;
}
Polynomial Polynomial::operator--()
{
coefficient[0]--;
return *this;
}
Polynomial& Polynomial::operator-- (int unused)
{
Polynomial copy(*this);
coefficient[0]--;
return copy;
}
尝试在 main 中执行此类操作时出现错误:
Polynomial p(
...一些矢量...);
cout << p++;