库函数如此,其他也一样。
#include <iostream> using namespace std; class time { public: int hour; int minute; int second; }; int show_time(time *t) //为什么会出错???? {//在使用time来定义t的时候,必须要在该文件下定义time;(这和所有的形式参数一样,先定义后使用) int m; for(m=0;m<2;m++) { cin>>t[m].hour>>t[m].minute >>t[m].second ;//在这里可以添加上hour minute second的范围 cout<<t[m].hour <<":"<<t[m].minute <<':'<<t[m].second <<endl; } return 0; }这是主函数
#include <iostream> #include "例题2.3子函数.cpp" using namespace std; int main() { int n; cin>>n; time *t=new time; show_time(t); return 0; }time必须在定义之后才能使用,也就是将class time放在子函数的文件夹中。