c++ - Serialize a class with a Qlist of custom classes as member (using QDataStream) -
i'm trying serialize class lesson(my custom class[i removed setters]), includes qlist(question custom class). during test serialization sigsegv(segmentation fault) when deserializing object of qlist inside class lesson.
upd1: serialized qlist* , changed qlist, sigsegv still appears.
class lesson { public: lesson(); lesson(qstring, qstring, qlist<question>); qstring getname() const; qstring gettext() const; qlist<question>* gettest() const; friend qdatastream &operator<<(qdatastream &out, const lesson &l){ out << l.getname() << l.gettext(); out << l.gettest(); return out; } friend qdatastream &operator>>(qdatastream &in, lesson &l){ qstring name; qstring text; qlist<question> t; in >> name >> text >> t; l = lesson(name, text, t); return in; } friend qdatastream &operator<<(qdatastream &out, const lesson *&l){ out << l->getname() << l->gettext(); return out; } friend qdatastream &operator>>(qdatastream &in, lesson *&l){ qstring name; qstring text; qlist<question> test; in >> name >> text;// >> test; l = new lesson(name, text, test); return in; } private: qstring text; qlist <question>* test_; qstring name; };
//class question starts here
class question { public: explicit question(qstring, qlist<qstring>, int type, qlist<int> ans); question(); friend qdatastream &operator <<(qdatastream& out, const question &q){ out << q.type << q.questtext << q.variants << q.answer; return out; } friend qdatastream &operator >>(qdatastream& in, question &q){ int type; qstring questext; qlist<qstring> vars; qlist<int>ans; in >> type >> q >> vars >> ans; q = question(questext, vars, type, ans); return in; } friend qdatastream &operator <<(qdatastream& out, const question *&q){ //out.setbyteorder(qsysinfo::byteorder); out << q->type << q->questtext << q->variants << q->answer; return out; } friend qdatastream &operator >>(qdatastream& in, question *&q){ //in.setbyteorder(qsysinfo::byteorder); int type; qstring questext; qlist<qstring> vars; qlist<int>ans; in >> type >> q >> vars >> ans; q = new question(questext, vars, type, ans); return in; } int gettype() const; qstring getquesttext() const; qlist<qstring> getvariants() const; qlist<int> getanswer() const; signals: public slots: private: int type; // see constants.h qstring questtext; qlist<qstring> variants; qlist<int> answer; };
// main.cpp test serialization
int main(int argc, char *argv[]) { qapplication a(argc, argv); core *c = new core; mainwindow w; qobject::connect(&w, signal(sendcheckuservaliditytocore(qstring,qstring)), c, slot(checkuservalidity(qstring,qstring))); qobject::connect(&w, signal(sendlessontocore(lesson*)), c, slot(savelesson(lesson*))); qobject::connect(&w, signal(requestlessonbyidfromcore(qstring)), c, slot(getlessonbyid(qstring))); qobject::connect(c, signal(returnlessonbyid(lesson*)), &w, signal(returnlessonbyidfromcore(lesson*))); qobject::connect(c, signal(entranceoff()), &w, slot(entranceoff())); qfile tfile("lessons/tfile.stc"); tfile.open(qiodevice::readwrite); qdatastream inout(&tfile); qlist<question> qlistq; question q; q.setquesttext("qwhy"); qlistq.insert(0, q); lesson lessont("hello", "welt", qlistq); lesson lessonout; inout << lessont; inout.device()->reset(); inout >> lessonout; qdebug() << lessonout.getname(); return a.exec(); }
you serialize qlist<question>*
, deserialize qlist<question>
. change operator<<
to:
friend qdatastream &operator<<(qdatastream &out, const lesson &l){ out << name << text << *test_; return out; }
Comments
Post a Comment