无法反序列化对象

编程入门 行业动态 更新时间:2024-10-23 09:21:26
无法反序列化对象 - Qt(Unable to deserialize object - Qt)

所以我像这样序列化我的对象,编译器没有显示这个警告:

QDataStream out(&file); out.setVersion(QDataStream::Qt_4_5); out << this->board;

在其他函数中,我想通过以下方式对其进行反序列化:

Board *board; QDataStream in(&file); in.setVersion(QDataStream::Qt_4_5); in >> board;

现在我不能这样做,因为我收到警告,说明运算符>>与那些操作数不匹配。 哪里有问题? 我按照指南: http : //doc.qt.io/qt-5/qtwidgets-tutorials-addressbook-part6-example.html 。

So I serialized my object like this, and the compiler show no warning for this:

QDataStream out(&file); out.setVersion(QDataStream::Qt_4_5); out << this->board;

And in other function I want to deserialize it by:

Board *board; QDataStream in(&file); in.setVersion(QDataStream::Qt_4_5); in >> board;

Now I cannot do that because I got warning that operator >> does not match that operands. Where is the problem? I followed guide on : http://doc.qt.io/qt-5/qtwidgets-tutorials-addressbook-part6-example.html.

最满意答案

好吧,首先,我不知道你是否意识到,但你实际上是在尝试从文件中读取指针( Board* )。 你通常不这样做,因为只要你没有释放它,指针就是好的。 有人可能已经给你这个文件,当生成的文件对你来说没用时, board在他们的计算机上。


现在,假设您确实想要读取对象( Board )而不是指针( Board* ):

我想Board是你自己创造的一个班级,对吗? 好吧,如果你没有覆盖operator>> ,当你要求它读取时,编译器不能知道猜测。 所以你应该覆盖operator>> 。

在您链接到的Qt页面中,它们不显示所有代码。 查看页面底部的链接,您将看到这一个 。 如您所见, contacts是一个QMap ,我假设它有一个重写的operator>> 。 这就是他们被允许in >> contacts 这里写in >> contacts 。

Well, first of, I don't know if you've realized, but you are actually trying to read a pointer (Board*) from the file. You generally don't do that, since the pointer is only good as long as you don't deallocate it. Somebody might have given you this file, and where board was located on their computer when the generated the file is pretty useless to you.


Now, assuming you actually wanted to read an object (Board) rather than a pointer (Board*) :

I suppose Board is a class you made yourself, correct? Well, if you don't override the operator>>, the compiler cannot know guess to do when you ask it to read it. So you should override operator>>.

In the Qt page you linked to, they do not show all of the code. Look at the links at the bottom of the page, you will see this one. As you can see, contacts is a QMap, which I assume has an overridden operator>>. This is why they are allowed to write in >> contacts here.

更多推荐

本文发布于:2023-07-30 22:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!