2

なぜエラーが発生するのか本当にわかりませんが、これもあまり得意ではありません。今は、レコード配列を印刷できない理由を理解しようとしています。誰かが私を正しい方向に向けることができると思いますか? 完成に近づいていないので、少しラフです...

#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;
}
4

1 に答える 1

8

Java などの言語とは異なり、C++ には何かを出力するための既定の方法がありません。使用coutするには、次の 2 つのいずれかを行う必要があります。

  1. 印刷可能なものへの暗黙的な変換を提供する (これを行わないでください)
  2. 次のように << 演算子をオーバーロードします。

    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
    }
    
于 2012-12-05T05:53:50.283 に答える