我创建了一个用于测试boost序列化库的小样本,但我有一个编译问题。
首先,这是代码:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <boost/filesystem/operations.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/version.hpp>
std::vector<uint8_t> buf;
class MyClass
{
public:
MyClass(){};
virtual ~MyClass(){};
int assetStatus;
friend class boost::serialization::access;
template<typename Archive> void serialize(
Archive & ar,
const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(assetStatus);
}
std::string ToString()
{
std::string toret;
toret += " assetStatus: " + assetStatus;
return toret;
}
};
int main()
{
MyClass a, b;
a.assetStatus = 10;
std::cout << a.ToString();
boost::archive::xml_oarchive ooxml(std::ofstream(dbPath));
ooxml << BOOST_SERIALIZATION_NVP(a); // error here
MyClass d;
boost::archive::xml_iarchive iixml(std::ifstream(dbPath));
iixml >> BOOST_SERIALIZATION_NVP(d); // error here
std::cout << d.ToString();
}
我收到了一行编译错误:
ooxml << BOOST_SERIALIZATION_NVP(a);
和
iixml >> BOOST_SERIALIZATION_NVP(d);
错误是:
与
中的operator>>
'iixml >> boost::serialization::make_nvp(const char*, T&) [with T=MyClass(((MyClass&)(&d)))]'
不匹配
你对这个含义有什么想法吗?
答案 0 :(得分:1)
看起来没有定义dbPath。此外,ooxml / iixml的声明似乎不正确。
尝试修改代码以执行以下操作: ...
const char * dbPath = "file.xml"
std::ofstream ofs(dbPath);
boost::archive::xml_oarchive ooxml(ofs);
ooxml << BOOST_SERIALIZATION_NVP(a);
std::ifstream ifs(dbPath);
boost::archive::xml_iarchive iixml(ofs);
iixml >> BOOST_SERIALIZATION_NVP(d);
答案 1 :(得分:0)
我认为NVP(名称值对)不支持阅读(即使用iixml),要么使用&amp; (而不是&gt;&gt;)或iixml&gt;&gt; d;