Derived class is not serialising/de-serialising...

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 Any help greatly appreciated. Thanks, -- Marcus A.T MacWilliam, Senior Software Engineer. 1Spatial Ltd. Tel: +44 (0)1223 420414, ext 2289, Mob: +44 (0)7803 706597. E-Mail: marcus.macwilliam@1spatial.com Skype:marcus.macwilliam Tennyson House, 7 Cambridge Business Park, Cambridge, CB4 0WZ. Registered in England No. 4785688, VAT Reg. No. 135539408. Website: http://www.1spatial.com/ E-mail: info@1spatial.com

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

I think you need to serialize the derive object by base pointer. e.g.: void Communication::sendRequest(BaseClass* request) // change to pointer here { // 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) // change to pointer here { // 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(); } -- View this message in context: http://boost.2283326.n4.nabble.com/Derived-class-is-not-serialising-de-seria... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (3)
-
Jeff Flinn
-
kwanti
-
Marcus MacWilliam