Boost logo

Boost Users :

From: Guy Létourneau (Guy.Letourneau_at_[hidden])
Date: 2006-08-07 13:02:15


Hello Robert,

Sorry for not giving you feedback earlier, I was on vacation. I've included your proposed changes in my test application but I still get the same compile error:

C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp: In function `void boost::archive::save(Archive&, T&) [with Archive = boost::archive::text_oarchive, T = const A*]':
C:/Boost/include/boost-1_33_1/boost/archive/basic_text_oarchive.hpp:78: instantiated from `void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = const A*, Archive = boost::archive::text_oarchive]'
C:/Boost/include/boost-1_33_1/boost/archive/detail/interface_oarchive.hpp:78: instantiated from `Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = const A*, Archive = boost::archive::text_oarchive]'
../main.cpp:101: instantiated from here
C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp:567: error: incomplete type `boost::STATIC_ASSERTION_FAILURE<false>' does not have member `value'
make: *** [main.o] Error 1

Could you tell me what I'm missing?

Thanks again for your help,

Guy Letourneau

Test application:

#include <iostream>
#include <fstream>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>

class A
{
    public:

        A()
        {
            i = 1;
        };

        virtual ~A()
        {
        };

        virtual void Dummy() = 0;

    private:
    
        int i;
        
        friend
        class boost::serialization::access;
        
        template <class Archive>
        void serialize( Archive& ar, const unsigned int version )
        {
            std::cout << "Serialization of A" << std::endl;
            std::cout.flush();
        }
};
BOOST_IS_ABSTRACT(A)

class B : public A
{
    public:

        B()
        {
        };

        virtual ~B()
        {
        };

        void Dummy()
        {
        };

        void SetValue( std::string& value )
        {
            Data = value;
        }

    private:

        friend
        class boost::serialization::access;

        template <class Archive>
        void serialize( Archive& ar, const unsigned int version )
        {
            ar & boost::serialization::base_object<A>( *this );
            ar & Data;
            std::cout << "Serialization of B" << std::endl;
            std::cout.flush();
        }

        std::string Data;
};
BOOST_CLASS_EXPORT(B)

int main()
{
    // Create a B object
    B* Child = new B;

    // Set some data
    std::string Data = "test";
    Child->SetValue( Data );
    
    // Assign it to a A ptr
    const A* Parent = Child; // Parent is now const
    
    // create and open a character archive for output
    std::ofstream ofs( "filename" );

    // save data to archive
    {
        try
        {
            boost::archive::text_oarchive oa( ofs );
            // write class instance to archive
            oa << Parent; // Ptr dereferencing is performed by the library
        }
        catch( boost::archive::archive_exception& e )
        {
            std::cout << "Save failed! : " << e.what();
            std::cout.flush();

            return false;
        }
        catch( ... )
        {
            std::cerr << "Unknown Exception " << std::endl;

            return false;
        }

    // archive and stream closed when destructors are called
    }

    delete Child;
}

-----Original Message-----
From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Robert Ramey
Sent: Saturday, July 22, 2006 6:43 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [Serialization] Problem serializingviabaseclassptr

Three show stopping problems in this code:

a) A doesn't have serialize method even though
such a method is invoked by base object. see:
 http://www.boost.org/libs/serialization/doc/serialization.html

b) saving a non-const object - see rationale section

c) Main show stopper:

> int main()
> {
> // Create a B object
> B* Child = new B;
>
> // Set some data
> std::string Data = "test";
> Child->SetValue( Data );
>
> // Assign it to a A ptr
> // const A* Parent = Child; // ****** note new const - see rationale
>
> // create and open a character archive for output
> std::ofstream ofs( "filename" );
>
> // save data to archive
> {
> try
> {
> boost::archive::text_oarchive oa( ofs );
> // write class instance to archive
               // *** big problem here. Dereferecing pointer to A yields a
ref
               // *** to an A - regardless that ptr was a B. What you
really want is:
                oa << Parent; // *** let serialization handle dererencing
                // *** instead of:
> //oa << *Parent;
> }
> catch( boost::archive::archive_exception& e )
> {
> std::cout << "Save failed! : " << e.what();
> std::cout.flush();
>
> return false;
> }
> catch( ... )
> {
> std::cerr << "Unknown Exception " << std::endl;
>
> return false;
> }
>
> // archive and stream closed when destructors are called
> }
>
> delete Child;
> }
>

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


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