我正在尝试序列化类Lesson(我的自定义类[我删除了一些setters]),其中包括一个QList(Question也是我的自定义类)。在测试序列化期间,当反序列化类Lesson中QList的任何对象时,我会得到一个SIGSEGV(分段错误)。
UPD1:我序列化了QList*,现在我把它改为QList,但SIGSEGV仍然出现。
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
{
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我在其中测试序列化
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();
}
序列化QList<Question>*
并反序列化QList<Question>
。将您的operator<<
更改为:
friend QDataStream &operator<<(QDataStream &out, const Lesson &l){
out << name << text << *test_;
return out;
}
我发现了问题所在。我只是没有初始化Question类的所有成员,这导致了segfault