一个有关重载运算符"<<"问题。
偶在帮别人 写程序时写了一个程序:可以我得重载“<<”出了错。。
帮我看看了。。
说说,注意的地方了。。。。
//student.h
// Student.h: interface for the Student class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_STUDENT_H__0D0FB24B_2043_4AC6_AF4D_F0E234797401__INCLUDED_)
#define AFX_STUDENT_H__0D0FB24B_2043_4AC6_AF4D_F0E234797401__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////student.h///////////////////////
#include <iostream>
#include <string>
using namespace std;
class Student
{
float score;
char *name;
char *id;
public:
Student(char *n,char *i,const float fSc);
~Student()
{
delete []name;
delete []id;
}
void ModifyScore(const float fSc);
void Output(ostream& out) const;
};
//student.cpp
#endif // !defined(AFX_STUDENT_H__0D0FB24B_2043_4AC6_AF4D_F0E234797401__INCLUDED_)
//student.cpp
// Student.cpp: implementation of the Student class.
//
//////////////////////////////////////////////////////////////////////
#include "Student.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Student::Student(char *n,char *i,const float fSc)
{
name=new char[strlen(n)+1];
strcpy(name,n);
id=new char[strlen(i)+1];
strcpy(id,i);
score=fSc;
}
inline void Student::ModifyScore(const float fSc)
{
score=fSc;
}
void Student::Output(ostream &out) const
{
out<<"ID:"<<id<<endl
<<"Name:"<<name<<endl
<<"Score:"<<score<<endl;
}
ostream& operator<<(ostream &out,const Student &x)
{
x.Output(out);
return out;
}
//main.cpp
//main.cpp
#include "student.h"
#include <iostream>
using namespace std;
void main()
{
Student s("558", "sum", 90);
s.Output(cout);
cout<<s;//问题所在
}