first post on Stackoverflow!
So to summarize my problem:
I am trying to serialize a custom class, which contains a map of another custom class (std::map).
So for serialization I have:
QDataStream& operator<<(QDataStream& out, const std::map<QString, MyCLass>& i){
out << static_cast<quint32>(i.size());
for (auto& elem : i)
{
out << elem.first;
out << elem.second;
}
return out;
}
And for deserialization, I have:
QDataStream& operator>>(QDataStream& in, std::map<QString, MyClass>& i){
quint32 mapSize;
in >> mapSize;
i.clear();
MyClass im; //Problem here, I do not have a default constructor
QString key;
while(mapSize--){
in >> key;
in >> im;
i.insert(std::pair<QString, MyClass>(key, im));
}
return in;
}
So the problem here is that I do not have (and do not really want) a default constructor for MyClass. Of course, I cannot just declare im just like this, and this leads me to my question: How would you go about serializing/deserializing this custom class ?
I have tried using malloc and a pointer:
MyClass* im = (MyClass*) malloc(sizeof(MyClass));
But the problem remains when inserting the MyClass objet in the map:
i.insert(std::pair<QString, MyClass>(key, *im));
raises the error: surchargesqdatastream.cpp:84:18: error: no matching constructor for initialization of 'std::pair'