0

按照本教程, 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;

但我得到 << operator no operator found 它采用 cArchive 类型的左手

4

2 回答 2

1

那是因为您没有operator<<为您的 class提供重载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
   }
}
于 2013-11-26T07:43:52.933 回答
0
afr << &e;

需要的指针类型。

于 2014-02-09T11:05:05.267 回答