使用运算符的<< >>没有运营商发现哪个采用cArchive类型的左手

时间:2013-11-26 01:54:54

标签: visual-c++ serialization mfc carchive

按照本教程,http://msdn.microsoft.com/en-us/library/vstudio/3bfsbt0t.aspx我实现了这段代码:

class Esame: public CObject
{

public:

INT voto;
INT crediti;
BOOL lode;
CString nome;
Esame(){}
Esame(CString nome, INT voto, BOOL lode, INT crediti) :nome(nome), voto(voto), lode    (lode), crediti(crediti) {}

void Serialize(CArchive& ar);

protected:
DECLARE_SERIAL(Esame)
};

IMPLEMENT_SERIAL(Esame, CObject, 1)

void Esame::Serialize(CArchive& ar){
CObject::Serialize(ar);
if (ar.IsStoring())
{       
    ar << voto << lode << crediti;
}
else
{       
    ar >> voto >> lode >> crediti;
}
}

然后我打电话给:

CFile file(_T("file.and"), CFile::modeCreate);
CArchive afr(&file, CArchive::store);
Esame e;
afr << e;

但是我明白了      &LT;&LT;运算符没有找到哪个运算符采用cArchive类型的左手

2 个答案:

答案 0 :(得分:1)

那是因为你没有为你的班级operator<<提供Esame的重载。您链接到的文章也没有这样做,所以也许您打算这样做:

CFile file(_T("file.and"), CFile::modeCreate);
CArchive afr(&file, CArchive::store);
Esame e;
e.Serialize(ar);

所以,你直接调用Serialize函数,你的类中的实现使用operator<<来序列化所需的原始成员变量,并在其他复杂对象上调用Serialize

正如教程所示:

void CCompoundObject::Serialize( CArchive& ar )
{
   CObject::Serialize( ar );    // Always call base class Serialize.
   m_myob.Serialize( ar );    // Call Serialize on embedded member.
   m_pOther->Serialize( ar );    // Call Serialize on objects of known exact type. 

   // Serialize dynamic members and other raw data 
   if ( ar.IsStoring() )
   {
      ar << m_pObDyn;
      // Store other members
   }
   else
   {
      ar >> m_pObDyn; // Polymorphic reconstruction of persistent object  
      //load other members
   }
}

答案 1 :(得分:0)

afr << &e;

需要指针类型。