c++相关查询中的类原型

class prototype in c++ related query

本文关键字:原型 查询 c++      更新时间:2023-10-16

如果我想在main()之后声明,如何在c++中为类提供原型?我写了以下代码片段。我参考了cplusplus.com上的材料,我试着在谷歌上搜索它,但找不到任何有用的东西。我错误地在main下面声明了这个类,但后来我意识到我没有给出它的原型,因此我的程序无法运行。

#include<iostream.h>
#include<conio.h>
void main()
{
    student s;
    s.show();
    getch();
}
class student
{
    int age;
    public:
    void show();
};
void student::show()
{
    age = 5;
    cout << age;
}

你不能。如果编写student s;,则student必须是完整类型。因此,远期申报是不够的。

显而易见的解决方案是在一个名为student.h#include的文件中编写类声明,该文件位于定义main()的文件的顶部。