I am running into an issue regarding serialization with MFC CArchive in a small project I am working on.
I got storing into the archive and then to a file working fine (I hope so) but reading from the file returns the object's properties as NULL.
Basically what I am trying to achieve is storing last up to 10 sales into an archive and loading back in when desired into a CTypedPtrArray
This is the Serialize function:
void MainStore::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsStoring())
{
int x = sales1.GetSize(), count = 1,k=0;
if (x <= 10)
{
ar << x;
for (int i = x - 1; i >=0; i--)
{
ar << sales1[i];
}
}
else
{
ar << 10;
for (int i = x - 1; count != 10; i--)
{
ar << sales1[i];
count++;
}
}
}
else
{
ar >> sizeOf10Records;
lastUpTo10Sales.RemoveAll();
for (int i = 0; i < sizeOf10Records; i++)
{
Sale* pNewSale;
ar >> pNewSale;
lastUpTo10Sales.Add(pNewSale);
}
}
}
And here is the MainStore and Sale classes properties :
class Sale :public CObject
{
DECLARE_SERIAL(Sale)
private:
Customer* customer;
Computer* computer;
char* dateAndTime;
class MainStore:public CObject
{
DECLARE_SERIAL(MainStore)
private:
Computer** computers;
//list<Sale> sales;
CTypedPtrArray<CObArray, Sale*> sales1;
CTypedPtrArray<CObArray, Sale*> lastUpTo10Sales;
int sizeOf10Records;
Both Sale and Mainstore are declared as serializable (Not sure about the latter if it is required) including the appropriate IMPLEMENT_SERIAL in the respective classes and there is an empty constructor for Sale (as well as MainStore) that I tried to leave empty or tried to initialize the properties as NULL but still no luck.
I have been trying for days to get serializing working and it seems I am missing just a little detail but cannot figure what that is.
Any help with regards to why I am receiving NULL will be much appreciated and if there is any missing info in my post I will edit it accordingly.
Edit:
So my Sale::Serialize function was indeed empty but when reading back from the archive I get an error that says that no operator ">>" matches these operands
void Sale::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsStoring())
{
ar << computer;
ar << customer;
ar << dateAndTime;
}
else
{
ar >> computer;
ar >> customer;
ar >> dateAndTime;
}
}
Edit 2:
Added Serialization to the Customer and Computer as well and edited
ar>>dateAndTime to ar>> *dateAndTime. Both the char* and and Customer classes no longer give me syntax error.
However Computer still does. Computer is an abstract class that inherits (as I read online on how to serialize abstract class) from CObject and has void Serialize function while the childs DECLARE/IMPLEMENT_SERIAL like so IMPLEMENT_SERIAL(ChildClass,Computer,1) .