1

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'

trablazar
  • 25
  • 4
  • 2
    Why don't you want to create a default constructor for `MyClass`? Also since you are using c++, I advise you to use new over malloc. Either way, creating `im` like that is a bad idea and won't work. – DSC Jun 09 '19 at 14:33
  • Hi DSC, thanks for the reply! How do you suggest I go about inserting my values from the QDataStream then ? The new over malloc is a good point, but immediately raises an error since I don't have a default constructor, while malloc just allocates the memory, which is not a problem – trablazar Jun 09 '19 at 14:41
  • I don't see why creating a default constructor would be a problem. It seems that would fix all your problems. – DSC Jun 09 '19 at 14:44
  • 2
    "while malloc just allocates the memory, which is not a problem" - that very much *is* a problem, since `malloc` does not call constructors and `free` does not call destructors. You are just hiding/deferring the problem. – Jesper Juhl Jun 09 '19 at 14:48
  • Well MyClass contains a const reference to another custom class, which does not have a default constructor either, which would require me to create a dummy object of said custom class, and I was told that such practice is not recommended! Is a default constructor the only way in your opinion ? – trablazar Jun 09 '19 at 14:51
  • @JesperJuhl Well you're right on that! So what would be your solution ? A default constructor ? – trablazar Jun 09 '19 at 14:55
  • 2
    @trablazar I would probably create a constructor that takes the stream the object should be created (deserialized) from. That constructor would then read from the stream and initialize its members or throw an exception if it could not do that from the stream given. – Jesper Juhl Jun 09 '19 at 14:59
  • @JesperJuhl Alright, sounds like a challenge, I'll try that! Thanks for the help – trablazar Jun 09 '19 at 15:18

0 Answers0