Boost logo

Boost Users :

From: George Verdad (gvv_at_[hidden])
Date: 2008-07-16 08:58:39


Hi All,

I am getting compiler errors using mingw 3.4.5 on the code below:
error: `BaseClass::BaseClass(const std::string&)' is protected

I tried to declare as friend the load_construct_data() override in BaseClass
but gave me the following error:
error: call of overloaded load_constuct_data(..) is ambiguous.

What am I doing wrong?

TIA,
Gvv

...
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/list.hpp>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

      template <class T>
      void Serialize(const T& t, std::string& OutData)
      {
         std::ostringstream ArchiveStream;
         boost::archive::text_oarchive Archive(ArchiveStream);
         Archive << t;
         OutData = ArchiveStream.str();
      }

      template <class T>
      void Deserialize(T& t, const std::string& InData)
      {
         std::istringstream ArchiveStream(InData);
         boost::archive::text_iarchive Archive(ArchiveStream);
         Archive >> t;
      }

class BaseClass : private boost::noncopyable
{
   public:
      virtual ~BaseClass() {}
      const std::string& GetName() const
      {
         return Name;
      }
   protected:
      BaseClass(const std::string& name) : Name(name) {}
   private:
      std::string Name;
      template<class Archive>
      void serialize(Archive& ar, const unsigned int version)
      {
         ar & Name;
      }
      friend class boost::serialization::access;
};
typedef boost::shared_ptr<BaseClass> BaseClassPtr;

class DerivedClass : public BaseClass
{
   public:
      DerivedClass(const std::string& name) : BaseClass(name) {}
   private:
      template<class Archive>
      void serialize(Archive& ar, const unsigned int version)
      {
         ar & boost::serialization::base_object<BaseClass>(*this);
      }
      friend class boost::serialization::access;
};
BOOST_CLASS_EXPORT(DerivedClass)

namespace boost
{
   namespace serialization
   {
      template<class Archive>
      inline void save_construct_data(Archive& ar, const BaseClass* t, const
unsigned int version)
      {
         ar << t->GetName();
      }

      template<class Archive>
      inline void load_construct_data(Archive& ar, BaseClass* t, const
unsigned int version)
      {
         std::string name;
         ar >> name;
         ::new(t)BaseClass(name);
      }

      template<class Archive>
      inline void save_construct_data(Archive& ar, const DerivedClass* t,
const unsigned int version)
      {
         ar << t->GetName();
      }

      template<class Archive>
      inline void load_construct_data(Archive& ar, DerivedClass* t, const
unsigned int version)
      {
         std::string name;
         ar >> name;
         ::new(t)DerivedClass(name);
      }
   }
}

int main()
{
   BaseClassPtr b(new DerivedClass("My name is gvv"));
   std::string data;
   std::cout << "Serialize" << std::endl;
   Serialize(b,data);
   std::cout << data << std::endl;

   b.reset();

   std::cout << "Deserialize" << std::endl;
   Deserialize(b,data);
   std::cout << b->GetName() << std::endl;
   return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net