2012-12-05 27 views
2

我真的不知道为什么即时通知错误,但我再也不是很好在这里,现在我只是想弄清楚为什么我不能打印出我的记录数组。认为任何人都可以将我指向正确的方向?它不接近完成,所以它有点粗糙...C2679:二进制'<<':没有找到操作符需要'学生'类型的右侧操作数(或者没有可接受的转换)

#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

您能否正确地缩进您的代码。 – jogojapan

+0

哦,它是最后一部分。 – user1877946

+0

哦是啊顺便说一句,你有一个内存泄漏 – jozefg

回答

8

与Java等语言不同,C++不提供默认的打印方式。为了使用cout你必须做的两件事情1:

  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 
    } 
    
相关问题