'a' : cannot access private member declared in class 'father',该如何解决
2012-09-08
a : cannot access private member declared in class fatherC/C++ code#includeiostreamusing name
'a' : cannot access private member declared in class 'father'
C/C++ code
#include<iostream>using namespace std;class father{public: father() { a = 0,b = 0; }private: int a,b;};class Child{public: Child() { c = 1,d = 1; }private: father gc; int c,d; friend void Print(Child & child);};void Print(Child & child){ cout<<child.gc.a<<" "<<child.gc.b<<endl;}int main(void){ Child test; Print(test); return 250;}
我今天看书上的解题的时候,碰见了这个问题,错误提示如下 Compiling... DosTest.cpp d:\vc 6.0test\dostest\dostest.cpp(36) : error C2248: 'a' : cannot access private member declared in class 'father' d:\vc 6.0test\dostest\dostest.cpp(12) : see declaration of 'a' d:\vc 6.0test\dostest\dostest.cpp(36) : error C2248: 'b' : cannot access private member declared in class 'father' d:\vc 6.0test\dostest\dostest.cpp(12) : see declaration of 'b' Error executing cl.exe.