ff.h
#ifndef __FF_H__
#define __FF_H__
class Y;
class X
{
public:
X(int i):i(i){}
friend void g(X &x);
friend class Z;
friend void h(X &x);
private:
int i;
};
class Y
{
public:
void g(X &x);
};
class Z
{
public:
void f(X &x);
void f1(X &x);
};
#endif
-----------------------------------------------------------------------------------------------------
ff.cpp
#include "ff.h"
#include <iostream>
using namespace std;
void Y::g(X &x)
{
x.i += 1;
cout << "Y::g() is A's friend function" << endl;
cout << "x.i : " << x.i << endl;
}
void Z::f(X &x)
{
x.i += 5;
cout << "Z is A's friend class" << endl;
cout << "Z::f() is called" << endl;
cout << "x.i : " << x.i << endl;
}
void Z::f1(X &x)
{
x.i += 6;
cout << "Z is A's friend class" << endl;
cout << "Z::f1() is called" << endl;
cout << "x.i : " << x.i << endl;
}
void h(X &x)
{
x.i += 10;
cout << "h is A's friend function" << endl;
cout << "x.i : " << x.i << endl;
}
--------------------------------------------------------------------------------------
在編譯ff.cpp的時候報如下錯誤:
In file included from ff.cpp:1:0:
ff.h: In member function 'void Y::g(X&)':
ff.h:15:6: error: 'int X::i' is private
int i;
^
ff.cpp:7:4: error: within this context
x.i += 1;
^
In file included from ff.cpp:1:0:
ff.h:15:6: error: 'int X::i' is private
int i;
^
ff.cpp:9:24: error: within this context
cout << "x.i : " << x.i << endl;
求指點
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/90192.html
標籤:基礎類
上一篇:miracl庫函式呼叫問題求助
