From your code to rebuild list:
for(int i = 0; i < list->size(); i++)
Problem is that when you're deserializing into an empty list size()
will return 0 (or a value completely unrelated to what you have to read from disk) and anyway you have to read items from stream, it doesn't matter actual list size. Moreoverlist is empty so you have first to create a new element (you can't just call at()
because there aren't elements inside). You can write an integer (for example) before items then read it when deserializing:
QDataStream &operator>>(QDataStream& in, FriendList* list)
{
int count = 0;
in >> count;
for(int i = 0; i < count; i++)
{
ContactData* contact = new ContactData();
in >> contact;
list->m_list->push_back(contact);
}
return in;
}
Do not forget to write count
in serialization function too. As alternative you may read everything until end of input stream:
QDataStream &operator>>(QDataStream& in, FriendList* list)
{
int count = 0;
in >> count;
while (!in.atEnd())
{
ContactData* contact = new ContactData();
in >> contact;
list->m_list->push_back(contact);
}
return in;
}
To finish check what Kamil said in comment, if you don't have any error during serialization probably it's just a typo but...