Problem with gbinary files, and QVector
-
@Loc888
Yes and the reason for the * in from of object pointer is that the header says
(QDataStream &, const YOUR_TYPE &); << referenceso if you give it the pointer without *, it
will save/stream the address of the object as it then matches << for int instead.
so say always use * when you have a pointer to an object with << -
Hi
Just to be sure
can you show your
QDataStream &operator<<(QDataStream &, const YOUR_Class &);
implementation.
(not the .h declaration) but the actual body of it. -
@mrjj In the QDataStream Class it looks like that, so i guess i am gonna use this template, except i am gonna use a custom data:
inline QDataStream &QDataStream::operator<<(quint16 i)
{ return *this << qint16(i); } -
Hi
Here is small sample.--in the .H file-- class MyClass { public: int value = 6; QString name = "test"; }; QDataStream &operator<<(QDataStream &out, const MyClass &); QDataStream &operator>>(QDataStream &in, MyClass &); ----------- --in the .CPP file-- QDataStream &operator<<(QDataStream &out, const MyClass &classRef) { return out << classRef.value << classRef.name; // save each member of your class } QDataStream &operator>>(QDataStream &in, MyClass &classRef){ return in >> classRef.value >> classRef.name; // read in IN SAME order } ------------------------------ //--test of saving-- { QFile file("e:/file.dat"); file.open(QIODevice::WriteOnly); QDataStream out(&file); MyClass *ptr = new MyClass; out << *ptr; } //--test of loading-- { QFile file("e:/file.dat"); file.open(QIODevice::ReadOnly); QDataStream in(&file); MyClass *otherptr = new MyClass; in >> *otherptr; }
-
@mrjj Something weird is happening, i declared those operators in a header file, then include it, and it's working, if i use that MyClass pointer, then i get the error..
-
@Loc888
What error? -
@mrjj Nope, it's ok... I just typed the wrong object type. It's working finnaly. Thanks
everybody for help. -
@Loc888
Good work. :)
please mark as solved then. -
@mrjj It's solved, but i don't wanna close it, maybe somebody will need help with that STD stuff, i leave it behind for now, because this motheod is cleaner and faster.
-
@Loc888 Please close. If somebody has a problem/question he/she can open a new thread. Your problem is solved, right?