帮忙指出错误指点指点谢了
#include<iostream>
using namespace std;
class X{
private:
static int i;
friend class Z;
friend void h(X&x1);
friend void g(X&x3);
}
class Z{
public:
void f(X&x2);
}
class Y{
public:
void g(X&x3);
}
void h(X&x1){
cout<<"friend h i="<<x1.i+5;
}
void Y::g(X&x3){
cout<<"friend g i="<<x3.i++;
}
void Z::f(X&x2){
cout<<"friend f i="<<x2.i+10;
}
int main(){
X x1,x2,x3;
h(x1);
Y y;
y.g(x3);
Z z;
z.f(x2);
system("pause");
return 0;
}
[解决办法]
#include<iostream>using namespace std;class X{ // private: public : //static成员属于整个类,需要定义public的 static int i; friend class Z; friend void h(X&x1); friend void g(X&x3); }; //需要加;的哈 //static成员需要在类外面定义 int X::i = 0; class Z{ public: void f(X&x2); }; class Y{ public: void g(X&x3); }; void h(X&x1){ cout<<"friend h i="<<x1.i+5; } void Y::g(X&x3){ cout<<"friend g i="<<x3.i++; } void Z::f(X&x2){ cout<<"friend f i="<<x2.i+10; } int main(){ X x1,x2,x3; h(x1); Y y; y.g(x3); Z z; z.f(x2); system("pause"); return 0; }