源程序如下:
#include <iostream>
using namespace std;
class Integer
{
int data;
public:
friend Integer& operator++(Integer&);
friend Integer& operator++(Integer&,int);
friend ostream& operator<<(ostream&,Integer&);
Integer(int i=0):data(i){cout<<"constructor of Integer"<<endl;}
};
Integer& operator++(Integer&i)
{
cout<<"pre"<<endl;
i.data++;
return i;
}
Integer& operator++(Integer&i,int j)
{
cout<<"post"<<endl;
Integer *n=new Integer(i.data);
i.data++;
return *n;
}
ostream& operator<<(ostream&out,Integer& i)
{
out<<i.data;
return out;
}
void main() {}
连接,出现错误如下:
--------------------Configuration: Z - Win32 Debug--------------------
Compiling...
main.cpp
F:\projects\Z\main.cpp(31) : error C2248: 'data' : cannot access private member declared in class 'Integer' // 这里双击后指示上面红色的部分
F:\projects\Z\main.cpp(7) : see declaration of 'data'
Error executing cl.exe.
Creating browse info file...
看了半天没觉得有什么不妥啊。谁能帮忙找一下错误吗,谢谢。