重载输入输出流,郁闷。
#include <iostream>#include <string>
using namespace std;
class Student{
public:
string szName;
string szID;
int age;
public:
Student(string a="0",string b="0",int c=0){szName=a;szID=b;age=c;}
friend istream& operator >>(istream&,Student&);
friend ostream& operator <<(ostream&,Student&);
};
istream& operator >>(istream& input,Student& st1)
{
cout<<"Please input the student's name,ID,age:";
input>>st1.szName>>st1.szID>>st1.age;
return input;
}
ostream& operator <<(ostream& output,Student& st1)
{
output<<"the name is:"<<st1.szName<<endl;
output<<"the ID is:"<<st1.szID<<endl;
output<<"the age is:"<<st1.age<<endl;
return output;
}
int main()
{
Student st1;
cin>>st1;
cout<<st1;
return 0;
}
--------------------Configuration: overload_cout - Win32 Debug--------------------
Compiling...
overload_cout.cpp
F:\C程序\Overload_cout\overload_cout.cpp(30) : error C2593: 'operator >>' is ambiguous
F:\C程序\Overload_cout\overload_cout.cpp(31) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
overload_cout.obj - 2 error(s), 0 warning(s)