4
votes

I am learning to use boost to serilaize some of my classes in C++.

I have a struct in an interface. I use the boost serilaize function to serialize this struct like this.

Interface.h

struct X {
  const Long rate;
}

template <class Archive>     
  void serialize(Archive& ar, uint32 version)
  {
     ar & this->rate;
  }
BOOST_CLASS_EXPORT(X);   

Now this header will be used by my main class. Let say main.cpp, Header for this main class has

Main.h

#include "Interface.h"
class Main {
 // some stuff

template <class Archive>
   void serialize(Archive& ar, uint32 version);

  X x;
  friend class boost::serialization::access;
};

template <class Archive>
void
Main::serialize(Archive& ar, uint32 version)
{
  ar & x;
}
BOOST_CLASS_EXPORT_KEY(Main);   

Main.cpp

BOOST_CLASS_EXPORT_IMPLEMENT(Main);

When I try to compile this code I get the following error:

C:\Users\boost1470_win64_vc90sp1\include\boost/archive/detail/check.hpp(162) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE' with [ x=false ]

C:\Users\boost1470_win64_vc90sp1\include\boost/archive/detail/iserializer.hpp(577) : see reference to function template instantiation 'void boost::archive::detail::check_const_loading(void)' being compiled with [ T=const Long

C:\Users\interfaces/Interface.h(12) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_iarchive::operator &(T &)' being compiled with [ Archive=boost::archive::text_iarchive, T=const Long ]

C:\Users\interfaces/Interface.h(17) : see reference to class template instantiation 'boost::archive::detail::extra_detail::guid_initializer' being compiled with [ T=X ]

Any idea what is going wrong. I am new to boost and figuring out how this works.

Note: If I use an int in the struct instead of long I do not see the boost::STATIC_ASSERTION_FAILURE error.

1
rate is const. How is deserialization going to set it to the right value?Alan Stokes
Please don't use "throw" to describe C++ compiler errors, in C++ "throw" refers to exceptions, not compiler errors.Jonathan Wakely
This is a typical error produced by trying to serialize to an archive that needs named value pairs. Try this code ` ar & make_nvp("rate", this->rate);` . Documentation: boost.org/doc/libs/1_59_0/libs/serialization/doc/… , but on a second though Alan's comment is most likely to be the error (const_cast might help).alfC

1 Answers

1
votes

To clarify the comment above

struct X {
    const Long rate;
}

is the problem, once X is instantiated, rate is set to a constant value, and boost cannot change it. If instead it reads

struct X {
    Long rate;
}

does your code work?