2013-11-26 86 views
0

按照本教程中,http://msdn.microsoft.com/en-us/library/vstudio/3bfsbt0t.aspx我实现这个代码:与运营商<< > CArchive的序列化>没有操作员发现这需要类型的左手的CArchive

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; 

但我得到 < <操作员找不到操作员找到哪个类型需要左手存档

回答

1

这是因为您没有为您的课程Esame提供operator<<的超载。您链接到不这样做无论是文章,那么也许你打算这样做,而不是:

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 
    } 
} 
+0

好,但是我失去了使用CArchive的“舒适性”:我可以简单地重载<< and >>作为任何祖先类; CArchive的帮助(我相信)会自动执行运算符的重载(我认为这是通过DECLARE_SERIAL和IMPLEMENT_SERIAL宏完成的)。手动超载<< >>通常是系统 – volperossa

0
afr << &e; 

指针类型需要的。

相关问题