Boost logo

Boost Users :

Subject: Re: [Boost-users] Derived class is not serialising/de-serialising...
From: Jeff Flinn (Jeffrey.Flinn_at_[hidden])
Date: 2013-08-05 09:44:41


On 8/5/2013 5:35 AM, Marcus MacWilliam wrote:
> Hello,
>
> I have the following 2 classes:
>
> >>BaseClass.h
>
> public BaseClass
> {
> private:
> int m_iMessageId;
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive &archive,
> const unsigned int version)
> {
> std::cout << "BOOST: Base Class" << std::endl; // TODO: remove this
> archive & m_iMessageId;
> }
> };
>
> >>DerivedClass.h
>
> public Derived : public Base
> {
> private:
> int m_iComPort;
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive &archive,
> const unsigned int version)
> {
> std::cout << "BOOST: Derived Class" << std::endl; // TODO: remove
> this
> archive &
> boost::serialization::base_object<CDeviceClientRequest>(*this)
> & m_iComPort;
> }
> };
>
> and the following 2 methods to send and receive:
>
> >>Communication.cc
>
> #include "DerivedClass.h"
> BOOST_CLASS_EXPORT(DerivedClass);
>
> void Communication::sendRequest(
> BaseClass& request)
> {
> // Open the file for write, and truncate.
> std::ofstream file(getFilename().c_str(), std::ios::trunc);
>
> // Use boost to serialize the object to the file.
> boost::archive::binary_oarchive archive(file);
> archive << request;
>
> // Close the file.
> file.close();
> }
>
> void Communication::receiveRequest(
> BaseClass& request)
> {
> // Open the file for read.
> std::ifstream file(getFilename().c_str());
>
> // Use boost to serialize the object from the file.
> boost::archive::binary_iarchive archive(file);
> archive >> request;
>
> // Close the file.
> file.close();
> }
>
> When I check the client and server the only output I see is:
>
> BOOST: Base Class
>
> However I am instantiating the derived class and passing it
> to the functions above. Why am I not seeing:
>
> BOOST: Base Class
> BOOST: Derived Class

Can you supply a minimially compiling example that demonstrates the
problem? At a minimum BaseClass has no virtual methods, and IIRC this
may cause problems.

Jeff


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