0

I am stuck with this problem while compiling my static class library.

I know that Boost doesn't officially support VS2012 but since this is my current development environment, I could really use some advice.

I have been searching around but nothing so far has helped me.

Sample code:

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
);
jt234
  • 662
  • 6
  • 16
  • What line of code gives the error? Also, shouldn't `ar & pImpl;` be `ar & (*pImpl);`? (pImpl is a pointer.) – David Schwartz Oct 11 '12 at 08:57
  • @David Schwartz It compiles now but it seems this is not the solution. Whenever I try to serialize a shared_ptr holding the foo in my main method, I get the same error there. – jt234 Oct 11 '12 at 09:55
  • What do you mean by the "same error"? The error you described is a compiler error and you say it compiles now. – David Schwartz Oct 11 '12 at 10:00
  • I use the static library in another project, which won't compile trying to serialize the shared pointer. – jt234 Oct 11 '12 at 10:15
  • Can you paste the code that doesn't compile? – David Schwartz Oct 11 '12 at 10:19
  • `std::stringstream ss; const shared_ptr foo; { boost::archive::text_oarchive oa(ss); oa << foo; } shared_ptr foo1; { boost::archive::text_iarchive ia(ss); ia >> foo1; }` – jt234 Oct 11 '12 at 10:28
  • `oa << foo;` should be `oa << (*foo);`. You want to serialize the object the pointer points to, not the pointer itself. (Serializing a pointer doesn't really make any logical sense.) – David Schwartz Oct 11 '12 at 10:51
  • Ow, made the same mistake the second time there. It works fine now. Can you make it an answer please? – jt234 Oct 11 '12 at 11:21

2 Answers2

1

boost::serialization is an extensible that can extended to work with any types, so you can implement your own version of load/save for std::shared_ptr, take a look at boost_installation_path/boost/serialization/shared_ptr.hpp and implement your own load/save from it. As another workaround you can use boost::shared_ptr in place of std::shared_ptr!! since you are using boost I see no advantage of using std::shared_ptr over boost::shared_ptr

BigBoss
  • 6,904
  • 2
  • 23
  • 38
1

You are trying to serialize pointers. You want to serialize the thing the pointer points to. The simplest way is to replace foo << ptr; with foo << (*ptr);.

The parentheses around *ptr are not necessary and many would see them as a sign of clumsiness. But if you find they make things clearer for you, use them.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Isn't the whole point of the boost serialization library that it *can* serialize pointers intelligently? Otherwise what is the point of `boost/serialization/shared_ptr.hpp`? – quant Sep 28 '13 at 23:38