2011-10-18 51 views
0

我创建了一个小样本来测试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); 

的错误是:

'iixml >> boost::serialization::make_nvp(const char*, T&) [with T=MyClass(((MyClass&)(&d)))]'

斗不过operator>>你有关于这个意思的任何想法?

回答

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); 
+0

是的,我在复制/粘贴时忘记了这个部分,但是它在我的代码中已经定义。关于另一部分,我已经以这种方式改变了它,并发现它是正确的解决方案,尽管我不明白哪个是不同的。 – cpl

0

我认为NVP(名称值对)不支持读(即iixml),要么使用&(而不是> >)或iixml >> d;