这个代码在编译时报错
#include <iostream>
using namespace std;
class teacher;
class student
{
public:
student() :num(0), age(0), gread(0) {}
student(int a, int b, int c);
void display(student* p,teacher s);
private:
int num;
int age;
int gread;
};
student::student(int a, int b, int c)
{
num = a;
age = b;
gread = c;
}
void student::display(student* p, teacher s)
{
cout << "教师:" << s.name << " " << s.post << " " << s.lesson << endl;
cout << "学生:" << endl;
cout << "学号" << " " << "年龄" << " " << "成绩" << endl;
for (int i = 0; i < 3; i++)
{
cout << p->num << " " << p->age << " " << p->gread << endl;
}
}
class teacher
{
public:
teacher() :name("NULL"), post("NULL"), lesson("NULL") {}
teacher(string n, string p, string l);
private:
friend void student::display(student* p, teacher s);
string name;
string post;
string lesson;
};
teacher::teacher(string n, string p, string l)
{
name = n;
post = p;
lesson = l;
}
int main()
{
teacher tea("涂", "讲师", "C++程序设计");
student stus[3] = { student(1401,18,83),student(1402,19,70),student(1403,20,98) };
stus[0].display(stus, tea);
return 0;
}
然后我把这个代码往后移了
student::student(int a, int b, int c)
{
num = a;
age = b;
gread = c;
}
void student::display(student* p, teacher s)
{
cout << "教师:" << s.name << " " << s.post << " " << s.lesson << endl;
cout << "学生:" << endl;
cout << "学号" << " " << "年龄" << " " << "成绩" << endl;
for (int i = 0; i < 3; i++)
{
cout << p->num << " " << p->age << " " << p->gread << endl;
}
}
成xia'm这样,编译运行都没有错,很疑惑为什么。
#include <iostream>
using namespace std;
class teacher;
class student
{
public:
student() :num(0), age(0), gread(0) {}
student(int a, int b, int c);
void display(student* p,teacher s);
private:
int num;
int age;
int gread;
};
class teacher
{
public:
teacher() :name("NULL"), post("NULL"), lesson("NULL") {}
teacher(string n, string p, string l);
private:
friend void student::display(student* p, teacher s);
string name;
string post;
string lesson;
};
student::student(int a, int b, int c)
{
num = a;
age = b;
gread = c;
}
void student::display(student* p, teacher s)
{
cout << "教师:" << s.name << " " << s.post << " " << s.lesson << endl;
cout << "学生:" << endl;
cout << "学号" << " " << "年龄" << " " << "成绩" << endl;
for (int i = 0; i < 3; i++)
{
cout << p->num << " " << p->age << " " << p->gread << endl;
}
}
teacher::teacher(string n, string p, string l)
{
name = n;
post = p;
lesson = l;
}
int main()
{
teacher tea("涂", "讲师", "C++程序设计");
student stus[3] = { student(1401,18,83),student(1402,19,70),student(1403,20,98) };
stus[0].display(stus, tea);
return 0;
}