华可乐 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 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B