C2679:二进制'<<' :找不到哪个运算符采用了'student'类型的右手操作数(或者没有可接受的转换)

时间:2012-12-05 05:49:22

标签: c++ class

我真的不知道为什么我得到了错误,但后来我不太擅长这个,现在我只想弄清楚为什么我不能打印出我的记录数组。想想任何人都能指出我正确的方向吗?它没有接近完成所以它有点粗糙......

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

1 个答案:

答案 0 :(得分:8)

与Java之类的语言不同,C ++不提供打印内容的默认方式。要使用cout,您必须执行以下两项操作之一:

  1. 提供隐式转换为可打印的内容(不要这样做)
  2. 重载&lt;&lt;像这样的运算符:

    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
    }