指针的向量.提升序列化
Vector of pointers. BOOST serialization
我想使用 BOOST 序列化/反序列化以下向量中对象的值(而不是指针):
std :: vector <A*> m_vector;
为了序列化,我使用以下代码:
int nItems = m_vector.size();
ar & nItems;
std::for_each(m_vector.begin(), m_vector.end(), [&ar](A* pItem) {
ar & *pItem;
});
并反序列化:
int nItems;
ar & nItems;
for (int i = 0; i < nItems; ++i) {
A* pItem;
ar & *pItem; ///////////// Run-Time Check Failure #3
m_vector.push_back(pItem);
}
但是当我运行该程序时,出现以下错误:
Run-Time Check Failure # 3 - The variable 'pItem' is Being Used without Being initialized.
我做错了什么?
谢谢。
您需要
为 pItem
指向的对象分配内存:
A* pItem = new A;
ar & *pItem;
m_vector.push_back(pItem);
错误是因为虽然你有一个指针,但在指针指向的内存位置没有对象 - 指针的值是垃圾(未初始化的指针)。
当您不再需要向量中指向 preven 内存泄漏的指针指向的对象时,不要忘记调用 delete
。 更好的是,使用智能指针(例如 boost::shared_ptr<>
),以确保在不再可访问时释放内存。
2 年后,但值得一提。
有一个更好的解决方案来序列化指向对象或任何其他 STL 容器(列表、集合等)的指针向量。要序列化向量,请添加:
#include <boost/serialization/vector.hpp>
然后你需要实现 serialize() 方法,并与 archieve 成为你的类朋友。在这个例子中解释了所有内容(仔细阅读所有评论,它们非常重要):
#include <fstream>
#include <iostream>
#include <vector>
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
class Abc{
// In order to make Abc serializable
// you need to friend this lass with serialization::access
friend class boost::serialization::access;
// and then add this method
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// choose what class fields do you want to serialize
ar & a;
ar & b;
ar & c;
}
public:
int a;
int b;
int c;
// don't forget about default constructor! It's necessary for serialization!
Abc(){};
Abc(int a, int b, int c): a(a), b(b), c(c){};
};
class GpsPosition
{
private:
// as mentioned above...
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
ar & wektorIntow;
}
int degrees;
int minutes;
float seconds;
public:
std::vector<Abc*> abcVector;
GpsPosition(){};
GpsPosition(int d, int m, float s): degrees(d), minutes(m), seconds(s)
{
// adding some objects to abcVector
abcVector.push_back(new Abc(1, 2, 3));
abcVector.push_back(new Abc(3, 2, 3));
abcVector.push_back(new Abc(2, 2, 3));
abcVector.push_back(new Abc(1, 2, 3));
}
int getDegrees(){ return this->degrees; }
int getMinutes(){ return this->minutes; }
float getSeconds(){ return this->seconds; }
};
int main(){
// And now how to use it
// Saving to file:
std::ofstream fileHandler("filename");
const GpsPosition position1(35, 59, 24.567f);
{
boost::archive::text_oarchive boostOutputArchieve(fileHandler);
boostOutputArchieve << position1;
}
// Reading from file:
GpsPosition newPosition;
{
std::ifstream fileHandler;
try{
fileHandler.open("filenacme");
boost::archive::text_iarchive boostInputArchieve(fileHandler);
// read class state from archive
boostInputArchieve >> newPosition;
// archive and stream closed when destructors are called
fileHandler.close();
}
catch (std::ifstream::failure e) {
std::cerr << "Exception opening/reading/closing file";
}
catch(boost::archive::archive_exception e){
std::cerr << "Exception opening/reading/closing file";
}
}
// print to the console
std::cout << newPosition.getMinutes() << std::endl;
std::cout << newPosition.abcVector[0]->a;
std::cin.get();
return 0;
}
有关更多信息,请查看本教程:http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html
相关文章:
- 如何在C++中序列化结构数据
- 序列化,没有库的整数,得到奇怪的结果
- 如何知道QDataStream不能反序列化某些内容
- 如何使用Python从C++中读取谷物序列化数据
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 自定义对象的dlib序列化在gcc中失败
- C++boost序列化多态性问题
- 增强基于 XML class_id的反序列化
- 提升反序列化对象具有 nan 或 -nan 值
- 在 cpp 中的平面缓冲区中序列化对象
- 每次进行继承时都需要提升::序列化::base_object吗?
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 提升序列化 1:73 的向后兼容性问题
- 将 boost 序列化对象的 asio::streambuf 表示转换为 Beast 的 DynamicBody req.body()
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 如何反序列化数组?
- 如何使用提升序列化?
- 序列化多晶型接口
- FlatBuffers/Protobuf 中是否有支持任意 24 位有符号整数定义的可移植二进制序列化架构?