boost序列化的编译错误

发布于 2024-12-11 00:31:05 字数 1934 浏览 0 评论 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>>中的operator>>不匹配boost::serialization::make_nvp(const char*, T&) [with T=MyClass(((MyClass&)(&d)))]'

你知道这是什么意思吗?

I created a small sample for testing the boost serialization library, but I have a compilation problem.

First of all, here's the code:

#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();
}

I get a compilation error at the lines:

ooxml << BOOST_SERIALIZATION_NVP(a);

and

iixml >> BOOST_SERIALIZATION_NVP(d);

The error is:

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

Do you have any idea regarding the meaning of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2024-12-18 00:31:05

看起来 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); 

It looks like dbPath is not defined. Additionally, the declaration of ooxml/iixml appears incorrect.

Try modifying your code to do the following:
...

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); 
衣神在巴黎 2024-12-18 00:31:05

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

I think NVP (name value pair) is not supported for reading (i.e. with iixml), either use & (instead of >>) or iixml >> d;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文