为什么总是访问不了X的私有数据成员?哪里出错了?

图片说明
main.cpp

#include <iostream>
#include "Classfile.h"
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//C++ P186 5-13

int main(int argc, char** argv) {
    X x(5);
    Y y;
    Z z;
    y.g(x);
    z.f(x);
    h(x);
    return 0;
}

Classfile.h


class X{
    public:
        friend void g(X x);
        friend class Z;
        friend void h(X x);
        X(int i=0):i(i){}
    private:
        int i;
};
class Y{
    public:
        void g(X x);
    private:
};

class Z{
    public:
         void f(X x);
    private:
        X x; 
};

Classfile.cpp

#include <iostream>
#include "Classfile.h"
using namespace std;
void Y::g(X x)
{
    x.i=x.i+1;
    cout<<"i="<<x.i<<endl;
 } 
void Z::f(X x)
{
    x.i=x.i+5;
    cout<<"i="<<x.i<<endl;
}
void h(X x)
{
    x.i=x.i+10;
    cout<<"i="<<x.i<<endl;
}
friend void g(X x); 改为 friend class Y;
加上
friend class Y;
因为
void Y::g(X x)
{
    x.i=x.i+1;
    cout<<"i="<<x.i<<endl;
 } 
你这个是在Y里面访问X的私有变量。