vector<struct> serialization / deserialization
-
wrote on 9 Feb 2018, 20:44 last edited by VRonin 2 Sept 2018, 21:07
Hi Gentlement,
I have ;
struct objectv1 { matrix<float, 0, 1 > obj_descriptor; string person_name; matrix<rgb_pixel> p_obj_chip; string notes; };
std::vector<objectv1> object_deteails;
How can I serialize and deserialize this in Qt world ?
stucked here for whole day ...
thx
-
wrote on 9 Feb 2018, 21:19 last edited by
you have to create:
QDataStream &operator<<(QDataStream &, const T&); //serialise QDataStream &operator>>(QDataStream &, T&); //de-serialise
for T being:
std::vector
string
matrix
rgb_pixel
objectv1
for example:
template<class T> QDataStream &operator<<(QDataStream& stream, const std::vector<T>& val){ stream << static_cast<quint32>(val.size()); for(auto& singleVal : val) stream << singleVal; return stream; } template<class T> QDataStream &operator>>(QDataStream& stream, std::vector<T>& val){ quint32 vecSize; val.clear(); stream >> vecSize; val.reserve(vecSize); T tempVal; while(vecSize--){ stream >> tempVal; val.push_back(tempVal); } return stream; } QDataStream &operator<<(QDataStream& stream, const objectv1& val){ return stream << val.obj_descriptor << val.person_name << val.p_obj_chip << val.notes; } QDataStream &operator>>(QDataStream& stream, objectv1& val){ return stream >> val.obj_descriptor >> val.person_name >> val.p_obj_chip >> val.notes; }
I don't know what the other classes are so i'll leave it to you.
-
wrote on 10 Feb 2018, 14:07 last edited by
I will try and let you know .
thnx a lot.
-
wrote on 10 Feb 2018, 18:01 last edited by
@VRonin said in vector<struct> serialization / deserialization:
QDataStream &operator<<(QDataStream& stream, const objectv1& val){
return stream << val.obj_descriptor << val.person_name << val.p_obj_chip << val.notes;
}
QDataStream &operator>>(QDataStream& stream, objectv1& val){
return stream >> val.obj_descriptor >> val.person_name >> val.p_obj_chip >> val.notes;
}it gave :
./widget.h:238:18: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
QDataStream &operator<<(QDataStream& stream, const pobjectv1& val){
^I am missing something definitely. being a java girl for years , c++ looks like alien sometimes.. :)
-
@VRonin said in vector<struct> serialization / deserialization:
QDataStream &operator<<(QDataStream& stream, const objectv1& val){
return stream << val.obj_descriptor << val.person_name << val.p_obj_chip << val.notes;
}
QDataStream &operator>>(QDataStream& stream, objectv1& val){
return stream >> val.obj_descriptor >> val.person_name >> val.p_obj_chip >> val.notes;
}it gave :
./widget.h:238:18: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
QDataStream &operator<<(QDataStream& stream, const pobjectv1& val){
^I am missing something definitely. being a java girl for years , c++ looks like alien sometimes.. :)
@RahibeMeryem said in vector<struct> serialization / deserialization:
it gave :
./widget.h:238:18: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
QDataStream &operator<<(QDataStream& stream, const pobjectv1& val){Show us what you got that generated that error, at least what is
pobjectv1
and how you defined the operators. It's rather hard and futile to guess. -
@RahibeMeryem said in vector<struct> serialization / deserialization:
it gave :
./widget.h:238:18: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
QDataStream &operator<<(QDataStream& stream, const pobjectv1& val){Show us what you got that generated that error, at least what is
pobjectv1
and how you defined the operators. It's rather hard and futile to guess.wrote on 12 Feb 2018, 17:21 last edited by RahibeMeryem 2 Dec 2018, 17:27@kshegunov Hi . here the codes:
struct personv1 { matrix<float, 0, 1 > obj_descriptor; string person_name; string notes; matrix<rgb_pixel> s_obj_chips; }; std::vector<personv1> person; and: template<class T> QDataStream &operator<<(QDataStream& stream, const std::vector<T>& val){ stream << static_cast<quint32>(val.size()); for(auto& singleVal : val) stream << singleVal; return stream; } template<class T> QDataStream &operator>>(QDataStream& stream, std::vector<T>& val){ quint32 vecSize; val.clear(); stream >> vecSize; val.reserve(vecSize); T tempVal; while(vecSize--){ stream >> tempVal; val.push_back(tempVal); } return stream; } QDataStream &operator<<(QDataStream& stream, const personv1& val){ return stream << val.obj_descriptor << val.person_name << << val.notes << val.s_obj_chips ; } QDataStream &operator>>(QDataStream& stream, personv1& val){ return stream >> val.obj_descriptor >> val.person_name >> val.notes >> val.s_obj_chips; }
-
wrote on 12 Feb 2018, 18:19 last edited by VRonin 2 Dec 2018, 18:19
You are missing the operators for :
matrix
,string
andrgb_pixel
I don't know what those classes are so i can't write them for you
1/7