在程序设计基础这本书中写的是:
后置自增表达式 Aobject++
成员函数重载的解释为
Aobject.operator++(0)
其中0是一个伪值
#include<iostream>
using namespace std;
class complex
{
private:
double real; //实部
double imag; //虚部
public:
complex &operator ++();
friend void output(complex &c);
friend void intput(complex &c);
};
complex &complex::operator++ ()
{
real=real+1;
imag=imag+1;
}
void output(complex &c)
{
cout<<"c++="<<"("<<c.real<<","<<c.imag<<"i)";
}
void intput(complex &c)
{
cin>>c.real>>c.imag;
}
int main()
{
complex c;
intput(c);
c++;
output(c);
}
在这段代码编译时会提示:
[Error] no 'operator++(int)' declared for postfix '++' [-fpermissive]
编译器是DEVC++,在vs2010下也会警告,是编译器的问题吗
Your sentence is an overloading of the prefix ++ operator
The overload declaration of the C++ post++ operator can be implemented by adding an int inside (), that is:
In addition, your overloaded operator does not have a return statement, and the main function does not return 0;
It is best to implement both the copy constructor and the copy assignment operator.