读书人

#039;a#039; : cannot access private member

发布时间: 2012-09-08 10:48:07 作者: rapoo

'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.

DosTest.exe - 2 error(s), 0 warning(s)
求高手解决

[解决办法]
探讨
#include<iostream>
using namespace std;

class Child;

class father
{
friend void Print(Child & child);

public:
father()
{
a = 0,b = 0;
}
private:
int a,b;

};


class Child
……

读书人网 >C++

热点推荐