Boost logo

Boost Users :

From: Van Kincaid (vkincaid_at_[hidden])
Date: 2006-07-12 13:23:34


Hi,

I just started learning to use serialization and having problems with
compiling for hash_map (map/deque/etc all work fine for me).

I found your discussion a while ago relating to this
http://thread.gmane.org/gmane.comp.lib.boost.user/12609/focus=12609

So I built my test program with

g++ -D__GLIBCPP__ -DBOOST_HAS_HASH
-DBOOST_STD_EXTENSION_NAMESPACE=__gnu_cxx -c test.cpp -o test.o

And I still have the following compile errors saying that serialize
function for hash_map is not defined. I also looked at the
preprocessor output and did find the definition for hash_map
serialize() function. It doesn't make sense to me why this happens.

Any help is greatly appreciated. The test file is attached at the end.

/usr/local/include/boost/serialization/access.hpp: In static member
function 'static void
boost::serialization::access::serialize(Archive&, T&, unsigned int)
[with Archive = boost::archive::text_oarchive, T =
__gnu_cxx::hash_map<int, boost::shared_ptr<DaoData>,
__gnu_cxx::hash<int>, std::less<int>,
std::allocator<boost::shared_ptr<DaoData> > >]':
/usr/local/include/boost/serialization/serialization.hpp:81:
instantiated from 'void boost::serialization::serialize(Archive&, T&,
unsigned int) [with Archive = boost::archive::text_oarchive, T =
__gnu_cxx::hash_map<int, boost::shared_ptr<DaoData>,
__gnu_cxx::hash<int>, std::less<int>,
std::allocator<boost::shared_ptr<DaoData> > >]'
/usr/local/include/boost/serialization/serialization.hpp:140:
instantiated from 'void boost::serialization::serialize_adl(Archive&,
T&, unsigned int) [with Archive = boost::archive::text_oarchive, T =
__gnu_cxx::hash_map<int, boost::shared_ptr<DaoData>,
__gnu_cxx::hash<int>, std::less<int>,
std::allocator<boost::shared_ptr<DaoData> > >]'
/usr/local/include/boost/archive/detail/oserializer.hpp:148:
instantiated from 'void boost::archive::detail::oserializer<Archive,
T>::save_object_data(boost::archive::detail::basic_oarchive&, const
void*) const [with Archive = boost::archive::text_oarchive, T =
__gnu_cxx::hash_map<int, boost::shared_ptr<DaoData>,
__gnu_cxx::hash<int>, std::less<int>,
std::allocator<boost::shared_ptr<DaoData> > >]'
test.cpp:157: instantiated from here
/usr/local/include/boost/serialization/access.hpp:109: error: 'class
__gnu_cxx::hash_map<int, boost::shared_ptr<DaoData>,
__gnu_cxx::hash<int>, std::less<int>,
std::allocator<boost::shared_ptr<DaoData> > >' has no member named
'serialize'

-----------------------------------
test.cpp

#include <fstream>
#include <map>
#include <iostream>
#include <ext/hash_map>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/hash_map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/serialization.hpp>

using namespace std;
using namespace __gnu_cxx;
using namespace boost;
using namespace boost::serialization;

template<typename DataType, typename KeyType, typename CompareType>
class DAO
{
        friend class serialization::access;

public:
        typedef shared_ptr<DataType> DataTypePtr;
        typedef pair<KeyType, DataTypePtr> KeyDataPair;
#if 0
        typedef map<KeyType, DataTypePtr> DataTypeMap;
#else
        typedef hash_map<KeyType, DataTypePtr, hash<KeyType>, CompareType> DataTypeMap;
#endif
        typedef typename DataTypeMap::iterator DataTypeMapIter;
        typedef typename DataTypeMap::const_iterator DataTypeMapConstIter;

        DAO() {}

        ~DAO() {}

        template <class Archive>
        void serialize( Archive& ar, const unsigned int version )
        {
                ar & m_dataTypeMap;
        }

        void insertData( DataTypePtr dataPtr )
        {
                m_dataTypeMap.insert( KeyDataPair( dataPtr->getKey(), dataPtr ) );
        }

        void display() const
        {
                DataTypeMapConstIter iter = m_dataTypeMap.begin();
                while ( iter != m_dataTypeMap.end() )
                {
                        iter->second->display();
                        iter++;
                }

        }

        void save_binary( const char* filename )
        {
                ofstream ofs(filename);
                  archive::text_oarchive oa(ofs);
                oa & *this;
        }

        bool load_binary( const char* filename )
        {
                try
                {
                        ifstream ifs( "test.dat", ios::binary );
                        archive::text_iarchive ia( ifs );
                        ia & *this;
                        display();
                        return true;
                }
                catch ( exception& e )
                {
                        cerr << "Exception caught: " << e.what() << endl;
                        return false;
                }
        }

private:
        DataTypeMap m_dataTypeMap;
};

class DaoData
{
public:
        int m_key;
        string m_keyStr;

        DaoData() {}

        DaoData( int key, const string& keyStr ) :
                m_key( key ),
                m_keyStr( keyStr )
        {
        }

        DaoData( DaoData& daoData )
        {
                m_key = daoData.m_key;
                m_keyStr = daoData.m_keyStr;
        }

        ~DaoData() {}

        int getKey() const { return m_key; }

        void display() const
        {
                cout << "key = " << m_key << endl;
                cout << "keyStr = " << m_keyStr << endl;
        }

        template <class Archive>
        void serialize( Archive& ar, const unsigned int version )
        {
                ar & m_key;
                ar & m_keyStr;
        }
};

void test_dao_serialize()
{
        DAO<DaoData, int, less<int> > myDao;
        shared_ptr<DaoData> dataPtr( new DaoData( 1, "ONE" ) );
        myDao.insertData( dataPtr );

        shared_ptr<DaoData> dataPtr2( new DaoData( 2, "TWO" ) );
        myDao.insertData( dataPtr2 );

        myDao.save_binary( "test.dat" );
        //myDao.load_binary( "test.dat" );
}

int main()
{
        test_dao_serialize();
        return 0;
}


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