gpt4 book ai didi

c++ - Visual Studio 2012 错误 C2039 : 'serialize' : is not a member of 'std::shared_ptr<_Ty>'

转载 作者:行者123 更新时间:2023-11-28 03:30:02 26 4
gpt4 key购买 nike

我在编译我的静态类库时遇到了这个问题。

我知道 Boost 不正式支持 VS2012,但由于这是我当前的开发环境,我真的可以使用一些建议。

我一直在四处寻找,但到目前为止没有任何帮助。

示例代码:

Foo.h:

#include "FooImpl.h"
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

class Foo
{
public:
Foo(void) : pImpl(std::make_shared<FooImpl>()) {}
//similar constructors follow

//a few get methods here
private:
std::shared_ptr<FooImpl> pImpl;

friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive & ar, const unsigned int file_version);
}

Foo.cpp:

#include "stdafx.h"
#include "Foo.h"

template<class Archive>
void Foo::serialize(Archive& ar, const unsigned int ver)
{
ar & pImpl;
}

template void Foo::serialize<boost::archive::text_iarchive>(
boost::archive::text_iarchive & ar,
const unsigned int file_version
);
template void Foo::serialize<boost::archive::text_oarchive>(
boost::archive::text_oarchive & ar,
const unsigned int file_version
);

FooImpl.h:

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/string.hpp>

class FooImpl
{
public:
FooImpl(void);
//other constructors, get methods

private:
//data members - unsigned int & std::wstring

friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive& ar, const unsigned int ver);
};

FooImpl.cpp:

#include "stdafx.h"
#include "FooImpl.h"

//function implementations

template <typename Archive>
void FooImpl::serialize(Archive& ar, const unsigned int ver)
{
ar & id_;
ar & code_;
}

//Later added, serialization requires these

template void FooImpl::serialize<boost::archive::text_iarchive>(
boost::archive::text_iarchive & ar,
const unsigned int file_version
);

template void FooImpl::serialize<boost::archive::text_oarchive>(
boost::archive::text_oarchive & ar,
const unsigned int file_version
);

最佳答案

您正在尝试序列化指针。你想序列化指针指向的东西。最简单的方法是替换 foo << ptr;foo << (*ptr); .

*ptr 两边的括号没有必要,许多人会将它们视为笨拙的标志。但是,如果您发现它们让您的事情变得更清晰,请使用它们。

关于c++ - Visual Studio 2012 错误 C2039 : 'serialize' : is not a member of 'std::shared_ptr<_Ty>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12835780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com