++重载问题
#include <iostream>
using namespace std;
class Test
{
public:
Test(int a = 0)
{
Test::a = a;
}
friend Test operator +(Test&,Test&);
friend Test& operator ++(Test&);
public:
int a;
};
Test operator +(Test& temp1,Test& temp2)//+运算符重载函数
{
//cout<<temp1.a<<"|"<<temp2.a<<endl;//在这里可以观察传递过来的引用对象的成员分量
Test result(temp1.a+temp2.a);
return result;
}
Test& operator ++(Test& temp)//++运算符重载函数
{
temp.a++;
return temp;
}
int main()
{
Test a(100);
Test c=a+a;
cout<<c.a<<endl;
c++; //!!!!!!!!!!!!!!!!!错误出现
cout<<c.a<<endl;
system("pause");
}
这样错误提示 说c++应该写++c
我把++的重载函数的返回类型 & 去掉
为什么不行?