华可乐 2020-04-20 00:39 采纳率: 0%
浏览 111
已采纳

关于C++类的一个细节上的小问题

这个代码在编译时报错

#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;
}
  • 写回答

1条回答 默认 最新

  • Huoon 2020-04-20 23:02
    关注

    前置类型声明只能用于指针或者引用。
    第一种情况,对于cout << "教师:" << s.name << " " << s.post << " " << s.lesson << endl; teacher类的类型不完整,需要在使用teacher类之前有teacher类的定义。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里