【问题标题】:Visual Studio 2012 error C2039: 'serialize' : is not a member of 'std::shared_ptr<_Ty>'Visual Studio 2012 错误 C2039:“序列化”:不是“std::shared_ptr<_Ty>”的成员
【发布时间】:2012-10-11 08:54:38
【问题描述】:

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

我知道 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
);

【问题讨论】:

  • 哪行代码给出了错误?另外,ar &amp; pImpl; 不应该是ar &amp; (*pImpl); 吗? (pImpl 是一个指针。)
  • @David Schwartz 现在可以编译,但似乎这不是解决方案。每当我尝试在我的 main 方法中序列化一个包含 foo 的 shared_ptr 时,都会出现同样的错误。
  • “同样的错误”是什么意思?您描述的错误是编译器错误,您说它现在可以编译。
  • 我在另一个项目中使用静态库,尝试序列化共享指针时无法编译。
  • 能不能把不能编译的代码贴一下?

标签: c++ boost visual-studio-2012 shared-ptr


【解决方案1】:

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

*ptr 周围的括号不是必需的,许多人会将它们视为笨拙的标志。但是,如果您发现它们让您更清楚,请使用它们。

【讨论】:

  • boost 序列化库的重点不就是它可以 智能地序列化指针吗?不然boost/serialization/shared_ptr.hpp有什么意义?
【解决方案2】:

boost::serialization 是一个可扩展的可扩展以适用于任何类型,因此您可以为std::shared_ptr 实现您自己的load/save 版本,看看boost_installation_path/boost/serialization/shared_ptr.hpp 并从中实现您自己的load/save .作为另一种解决方法,您可以使用boost::shared_ptr 代替std::shared_ptr!!因为您使用的是boost,所以我认为使用std::shared_ptr 比使用boost::shared_ptr 没有优势

【讨论】:

    猜你喜欢
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2016-03-08
    • 2019-11-12
    相关资源
    最近更新 更多