0

I have a func which returns a const& to an Object. Let say,

class Y {
    private:
        Id id;
    public:
        Id const& GetId() const;
}  

Now this Y Object is part of another class, let say class X, which needs to be serialized by boost.

class X {
    template <class Archive>
    void serialize(Archive& ar, uint32 version)
     {
         ar & y->GetId();
     }
     Y y;
}

However, boost complains about this and gives error:

error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'


C:\Users\Y.h(580) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_iarchive<Archive>::operator &<const Id>(T &)' being compiled
            with
            [

How can I serialize the Id?

olevegard
  • 5,294
  • 1
  • 25
  • 29
Deepti Jain
  • 1,901
  • 4
  • 21
  • 29
  • Perhaps a review of the [Archive class](http://www.boost.org/doc/libs/1_54_0/libs/serialization/doc/archives.html) usage would be beneficial. – WhozCraig Aug 22 '13 at 17:14
  • does `Id` have a `serialize` function? – David Brown Aug 22 '13 at 17:15
  • Also can you post the entire compiler error message? – David Brown Aug 22 '13 at 17:16
  • Yes. It does. Its part of a big chunk of code. But we do have logic to serialize Id. The compiler error is: C:\Users\boost\windows\include\boost/archive/detail/check.hpp(162) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE' with [ x=false ] C:\Users\boost\windows\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 Id ] – Deepti Jain Aug 22 '13 at 17:18
  • The error message should contain a description of why the static assertion failed; what does it say? Does your real code incorrectly try to apply `->` to an object? Could we see the serialisation function for `Id`? – Mike Seymour Aug 22 '13 at 17:20
  • Id is just a typedef'd string. This is how it is serialized. template inline void serialize(Archive& ar, Id& id, uint32 version) { ar & static_cast(id); } – Deepti Jain Aug 22 '13 at 17:23

3 Answers3

4
 ar & y->GetId(); 

should be

  ar & y.GetId();
fatihk
  • 7,789
  • 1
  • 26
  • 48
  • Agreed. I actually use a reference to object Y in my code. So it is -> But for the given snippet it shud be y. instead of y-> – Deepti Jain Aug 22 '13 at 17:36
1

The purpose of boost serialization is to use references to serialize to an Archive.

Problem is it receives the values as references and not CONST references.

Id const& GetId() const; // Returns a const reference

Try using the following

Id & GetId(); 

And then one of the following

ar & y.GetId();
ar << y.GetId();

If you don't like the breaking of encapsulation then either use protected, friends, or copy it to another value and pass the value as reference

Id copy = y.GetId();
ar & copy;
Claudiordgz
  • 3,023
  • 1
  • 21
  • 48
0

Besides the issue -> (see @thomas) you need to have two:

Id const& GetId() const;
Id& GetId();

Or you do BOOST_SERIALIZATION_SPLIT_MEMBER input/output into two.