gpt4 book ai didi

c++ - 使用自定义类的 Qlist 作为成员序列化一个类(使用 QDataStream)

转载 作者:行者123 更新时间:2023-11-28 06:55:46 24 4
gpt4 key购买 nike

我正在尝试序列化 Lesson 类(我的自定义类 [我删除了一些 setter]),其中包括一个 QList(问题也是我的自定义类)。在测试序列化期间,反序列化 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;
}

关于c++ - 使用自定义类的 Qlist 作为成员序列化一个类(使用 QDataStream),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23202667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com