我真的不知道为什么我得到错误,但后来我不太擅长这个,现在我只是想弄清楚为什么我不能打印出我的记录数组.想想任何人都能指出我正确的方向吗?它没有接近完成所以它有点粗糙......
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class student
{
private:
int id, grade;
string firstname, lastname;
public:
student();
student(int sid, string firstn, string lastn, int sgrade);
void print(student* records, int size);
};
void main()
{
string report="records.txt";
int numr=0 , sid = 0,sgrade = 0;
string firstn,lastn;
student *records=new student[7];
student stu;
student();
ifstream in;
ofstream out;
in.open(report);
in>>numr;
for(int i=0; i>7; i++)
{
in>>sid>>firstn>>lastn>>sgrade;
records[i]=student(sid, firstn,lastn,sgrade);
}
in.close();
stu.print(records, numr);
system("pause");
}
student::student()
{
}
student::student(int sid, string firstn, string lastn, int sgrade)
{
id=sid;
firstname=firstn;
lastname=lastn;
grade=sgrade;
}
void student::print(student* records, int size)
{
for(int i=0; i>7; i++)
cout<<records[i]<< endl;
}
Run Code Online (Sandbox Code Playgroud)
与Java之类的语言不同,C++不提供打印内容的默认方式.为了使用cout
你必须做两件事之一:
像这样重载<<运算符:
ostream& operator <<(ostream& str, const student& printable){
//Do stuff using the printable student and str to print and format
//various pieces of the student object
return str;
//return the stream to allow chaining, str << obj1 << obj2
}
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
3179 次 |
最近记录: |