- #include <iostream>
- using namespace std;
- class CBase
- {
- private:
- int m_a;
- protected:
- int m_pp;
- public:
- CBase(int a,int pp): m_a(a),m_pp(pp){}
- friend ostream& operator<<(ostream &out, const CBase &b);
- };
- ostream& operator<<(ostream &out, const CBase &b)
- {
- out<<b.m_a<< " "<<b.m_pp<<endl;
- return out;
- }
编译时,却提示
error C2248: 'm_a: cannot access private member declared in class CBase
error C2248: 'm_pp: cannot access private member declared in class CBase
这就奇怪了,友元函数不是能访问类的私有成员变量吗?怎么就不行了。
后来得知,原来我用的是VC++6.0编译器,在vs2005上就没问题了。或者把头文件包含写成
#include <iostream.h>
//using namespace std
就可以了。