输出流和输入流的重载问题
一般情况下输入流和输出流是定义为该类的友元函数
我尝试在类内重载
virtual ostream& operator<<(ostream& out)
{
out<<m_nID<<" "<<m_strName.GetBuffer(MAX_PATH);
m_strName.ReleaseBuffer();
return out;
}
virtual istream& operator>>(istream& in)
{
UINT m_nTemp;
char strTemp[1024];
in>>m_nTemp>>strTemp;
m_nID=m_nTemp;
m_strName=strTemp;
return in;
}
这是一个CEntity的类内
CPlayer 从CEntity中派生
virtual ostream& operator<<(ostream& out)
{
out<<m_nID<<" "<<m_strName.GetBuffer(MAX_PATH);
m_strName.ReleaseBuffer();
return out;
}
virtual istream& operator>>(istream& in)
{
UINT m_nTemp;
char strTemp[1024];
in>>m_nTemp>>strTemp;
m_nID=m_nTemp;
m_strName=strTemp;
return in;
}
我这使用了虚函数,
要不就会发现在对CPlayer进行输入输出流时会只调用CEntity的重载函数;
编译时出错
ompiling...
Game.cpp
E:\mud\3\Game.cpp(48) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class CPlayer' (or there is no acceptable conversion)
E:\mud\3\Game.cpp(84) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CPlayer' (or there is no acceptable conversion)
ifile>>m_Player;
其中ifile是一个输入文件
我这样的处理方式只是尝试的这样处理,因为我很想把所有的运输封装在类内,请大家提提已经
类内这样定义重载的是输入流和输出流的运算啊