"unregistered void cast" using boost serialiazation derived to base class

user249806

I am on my second attempt to setup polymorphic serialization using the boost library. I am using this as a learning experience, but I may be a little in over my head and I am considering going back to coding the serialization myself rather than boost. Or switch to learning the vistor message Sehe showed me in a previous post.

The issue I am seeing is "unregistered void cast"

I am using shared library linking for the boost serialization library

aTodo.h:

#ifndef ATODO_H
#define ATODO_H
#include <boost/serialization/export.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/unique_ptr.hpp>

#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>

class aTodo{
  public: 
   static const unsigned _Type=0x00;
   virtual ~aTodo(){};
   virtual void Do()=0;
   virtual unsigned getInitType(){return _Type;};

   private:
   friend class boost::serialization::access;
   template  <class Ar> void serialize(Ar &, unsigned){};
};
#endif

todoExec.h:

 #ifndef ATODOEXEC_H
#define ATODOEXEC_H
#include "aTodo.h"
class todoExec : public aTodo{
   public:
   static const unsigned _TYPE= 0x01;
      todoExec(std::string const & command=""):_command(command){};
      virtual unsigned getInitType(){return  _TYPE;};
      virtual void Do(){std::cout << "foo:" << getCommand() << std::endl;};
      std::string getCommand() const {return _command;};
   protected:

   private: 
      friend class boost::serialization::access;
      template <class Archive> void serilize(Archive & ar, unsigned){
         boost::serialization::void_cast_register<todoExec,aTodo>();
         boost::serialization::base_object<aTodo>(*this);
         ar& _command;
      }  
   std::string _command;
};
#endif

todoFactory.h:

#ifndef TODOFACTORY_H
#define TODOFACTORY_H

#include "todoExec.h"
#include <memory>
class todoFactory{

    todoFactory()=default;
   public:
      static std::unique_ptr<todoFactory> create(){return std::move(std::unique_ptr<todoFactory>(new todoFactory));};

      //save
      static std::string save(std::unique_ptr<aTodo> &todoIn){
         std::string out;
         {  
            boost::iostreams::stream<boost::iostreams::back_insert_device<std::string>>os(out);
            boost::archive::text_oarchive archive(os);

            archive << todoIn;
         }  
         return out;

      }  

      static std::unique_ptr<aTodo> load(std::string const &s ){ 
         std::unique_ptr<aTodo> p; 
         {  
            boost::iostreams::stream<boost::iostreams::array_source> is(boost::iostreams::array_source{s.data(),s.size()});
            boost::archive::text_iarchive archive(is);
            archive >> p; 
         }  
         return std::move(p);
       }  
       std::unique_ptr<aTodo> createExec(std::string command) {return std::unique_ptr<aTodo>(new todoExec(command));};

};
#endif

client.cpp

#include <string>
#include <iostream>
#include "todoFactory.h"
BOOST_SERIALIZATION_ASSUME_ABSTRACT(aTodo)
BOOST_CLASS_EXPORT(todoExec)


#include <memory>

int main(void)
{
  char mtype=0x01;
   std::string dataToSend = "ls -al /home/ajonen";

   auto tmpTodoFactory=todoFactory::create(); //create factory
   auto anExecTodo=tmpTodoFactory->createExec(dataToSend); //create ExecTodo from factory
   std::string toSend= tmpTodoFactory->save(anExecTodo);
  return 0;
}

The error I get is:

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  unregistered void cast 8todoExec<-5aTodo
Aborted
doqtor

In class todoExec you've got a typo - is: serilize, should be: serialize; therefore the cast is not registered.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Derived Class In a Base Method

From Dev

Python: Using derived class attributes in base class

From Dev

class derived from class template using base:base in body

From Dev

Using/storing derived member in derived class with base class that stores base member

From Dev

Inheritance : using base class or derived class to do stuff

From Dev

Use typedef/using from templated base class in derived class

From Dev

Call derived class method when using base class in foreach loop

From Dev

Conflict in return type from base class with derived class using auto

From Dev

Call templated function with derived class arguments using base class pointers

From Dev

How to return a derived class using only code in the base class?

From Dev

Base class static method using constants of derived class

From Dev

C++ using static array in base class, declaring in derived class

From Dev

Creating derived class instance using base class instance

From Dev

Base class static method using constants of derived class

From Dev

base pointer to derived class

From Dev

Cast derived class to base

From Dev

Constructor in base and derived class

From Dev

Boost serialization of template derived class

From Dev

Casting derived class to base class keeps knowledge of derived when using generics

From Dev

using-declaration in derived class does not hide same function derived from base class

From Dev

Convert a base class to a derived class

From Dev

composition of a derived class in a base class

From Dev

Base class to Derived class in Iteration

From Dev

composition of a derived class in a base class

From Dev

Using base class operators for derived classes in C++11

From Dev

Static Polymorphism with CRTP: Using the Base Class to Call Derived Methods

From Dev

How to overload operator << for derived classes using a shared base class?

From Dev

Using an interface as a field type but have the base class pick the derived struct

From Dev

Instantiating objects in an array using base class and derived classes in C#

Related Related

  1. 1

    Using Derived Class In a Base Method

  2. 2

    Python: Using derived class attributes in base class

  3. 3

    class derived from class template using base:base in body

  4. 4

    Using/storing derived member in derived class with base class that stores base member

  5. 5

    Inheritance : using base class or derived class to do stuff

  6. 6

    Use typedef/using from templated base class in derived class

  7. 7

    Call derived class method when using base class in foreach loop

  8. 8

    Conflict in return type from base class with derived class using auto

  9. 9

    Call templated function with derived class arguments using base class pointers

  10. 10

    How to return a derived class using only code in the base class?

  11. 11

    Base class static method using constants of derived class

  12. 12

    C++ using static array in base class, declaring in derived class

  13. 13

    Creating derived class instance using base class instance

  14. 14

    Base class static method using constants of derived class

  15. 15

    base pointer to derived class

  16. 16

    Cast derived class to base

  17. 17

    Constructor in base and derived class

  18. 18

    Boost serialization of template derived class

  19. 19

    Casting derived class to base class keeps knowledge of derived when using generics

  20. 20

    using-declaration in derived class does not hide same function derived from base class

  21. 21

    Convert a base class to a derived class

  22. 22

    composition of a derived class in a base class

  23. 23

    Base class to Derived class in Iteration

  24. 24

    composition of a derived class in a base class

  25. 25

    Using base class operators for derived classes in C++11

  26. 26

    Static Polymorphism with CRTP: Using the Base Class to Call Derived Methods

  27. 27

    How to overload operator << for derived classes using a shared base class?

  28. 28

    Using an interface as a field type but have the base class pick the derived struct

  29. 29

    Instantiating objects in an array using base class and derived classes in C#

HotTag

Archive