问题:
vc6
c++ 重载 << 时遇到 “ cannot access private member declared in class ‘test’”
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream> using namespace std; class test{ private: int i; float f; char ch; ; public: test(int a=0,float b=0,char c='\0'){i=a;f=b;ch=c;} friend ostream &operator<<(ostream &os,const test &c); }; ostream &operator<<(ostream &os,const test &c){ os<<c.i; return os; } void main(){ test t; cout<< t; } |
解决:
把
|
ostream &operator<<(ostream &os,const test &c) |
移动到 class 里边
|
friend ostream &operator<<(ostream &os,const test &c){ os<<c.i; return os; } |
参考:
http://bbs.csdn.net/topics/390151210