顺序应该是这样:
//class Teacher; //这里编译不成功,提示incomplete type 'Teacher' named in nested name specifier
class Student;//add
class Teacher
{
public:
void printStudentInTeacher(Student &s);
};
class Student
{
friend void printStudent(Student grade);
friend void Teacher::printStudentInTeacher(Student &s);
public:
private:
int grade;
};
void printStudent(Student s)
{
cout << "outer function print:";
cout << s.grade << endl;
}
void Teacher::printStudentInTeacher(Student &s)
{
cout << "Teacher print:";
cout << s.grade << endl;
}
worker(){}默认构造函数,其作用是把类的私有成员变量初始化为string类的默认值。
也就是空字符串。
~worker(){}是析构函数,其作用是在超出对象的作用域后,将对象所占的内存给释放掉。
避免物理内存被用尽
#include
using namespace std;
class Student;
class Teacher
{
public:
void printStudentInTeacher(Student &s);
};
class Student
{
//public:
friend void printStudent(Student grade);
friend void Teacher::printStudentInTeacher(Student &s);
private:
int grade;
};
void printStudent(Student s)
{
cout<<"outer function print:";
cout << s.grade << endl;
}
void Teacher::printStudentInTeacher(Student &s)
{
cout <<"Teacher print:";
cout << s.grade << endl;
}
顺序应该是这样子的