发布网友 发布时间:2022-04-21 06:59
共2个回答
热心网友 时间:2023-09-21 06:51
#include<iostream>
using namespace std;
class Boy; //对类声明就可以了
class Girl{
char *name;
int age;
public:
Girl(char n[], int a){ name= n; age = a; }
~Girl(){cout << "delet girl " << '\n'; }
void display(){ cout << name << " " << age << '\n'; }
void visitBoy( Boy &b);
};
class Boy{
char *name;
int age;
public:
Boy(char n[], int a){ name=n; age = a; }
~Boy(){
cout << "delet boy" << '\n';
}
void display(){
cout << name << " " << age << '\n';
}
friend class Girl;
};
void main(){
Girl g("Mary", 16);
Boy b("Tom", 18);
g.display();
b.display();
g.visitBoy(b);
}
void Girl::visitBoy(Boy &b){
cout << b.name << " " << b.age;
}
热心网友 时间:2023-09-21 06:51
在class Girl {前加个class Boly声明一下即可。