Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:54:54+00:00 2026-05-27T10:54:54+00:00

I’m trying to serialize my data structures in order to write them to a

  • 0

I’m trying to serialize my data structures in order to write them to a tcp socket.

So far I found that my problem is the serialization. I even tried to use

BOOST_SERIALIZATION_ASSUME_ABSTRACT(T)

but I can’t find any working example similar to my program and how to implement it correctly.

Here are some of the links that I have visited:

  • http://programmers-blog.com/category/c-c
  • http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/tutorial.html#simplecase
  • http://en.highscore.de/cpp/boost/serialization.html#serialization_class_hierarchies

My data structures are a little more complicated then this one but let’s assume that I have the following structure

Coordinate.h

#include <boost\archive\text_iarchive.hpp>
#include <boost\archive\text_oarchive.hpp>

class Coordinate {
public:
    Coordinate() {}
    Coordinate(int c) : c(c) {}
    int get(void) { return c; }
    std::string toString(void);
private:
    int c;
    friend class boost::serialization::access;
    template<typename Archive>
    void serialize(Archive &ar, const unsigned int version) {
        ar & this->c;
    }
};

Move.h

class Coordinate;

#include "Coordinate.h"

#include <boost\archive\text_iarchive.hpp>
#include <boost\archive\text_oarchive.hpp>

class Move {
public:
    Move() {}
    ~Move() {}
    Coordinate* getCoordinate(void) {return this->destination; }
    virtual bool isJump(void) = 0;
protected:
    Coordinate *destination;
private:
    friend class boost::serialization::access;
    template<typename Archive>
    void serialize(Archive &ar, const unsigned int version) {
        ar & this->destination;
    }
};

MoveNormal.h

class Coordinate;

#include "Move.h"
#include "Coordinate.h"

#include <boost\archive\text_iarchive.hpp>
#include <boost\archive\text_oarchive.hpp>

class MoveNormal : public Move {
public:
    MoveNormal() {}
    MoveNormal(Coordinate *destination) { this->destination = destination; }
    ~MoveNormal() {}
    virtual bool isJump(void);
private:
    friend class boost::serialization::access;
    template<typename Archive>
    void serialize(Archive &ar, const unsigned int version) {
        ar & boost::serialization::base_object<Move>(*this);
    }
};

The virtual methods are defined in here.

MoveNormal.cpp

#include "MoveNormal.h"

bool MoveNormal::isJump(void) {
    return false;
}

My main.cpp looks like this:

#include "Coordinate.h"
#include "Move.h"
#include "MoveNormal.h"

#include <fstream>

#include <boost\archive\text_iarchive.hpp>
#include <boost\archive\text_oarchive.hpp>

int main(int argc, char *argv[]) {
    Coordinate *c = new Coordinate(10);
    // This runs OK
    /*
    {
        std::ofstream ofs("f.txt");
        boost::archive::text_oarchive oa(ofs);
        oa << c;
    }
    Coordinate *d;
    {
        std::ifstream ifs("f.txt");
        boost::archive::text_iarchive ia(ifs);
        ia >> d;
    }
    std::cout << "c.get(): " << c->get() << std::endl;
    std::cout << "d.get(): " << d->get() << std::endl;
    */

    // This is where I get my error
    Move *m  = new MoveNormal(c);
    {
        std::ofstream ofs("f.txt");
        boost::archive::text_oarchive oa(ofs);
        oa << m;    // Line where the error occurs
    }
    return 0;
}

But when I run the program I get the following error:

Unhandled exception at 0x76dbb9bc in Test.exe: Microsoft C++ exception: boost::archive::archive_exception at memory location 0x001df078..

I’m using VS2010, and Boost 1.48.0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:54:55+00:00Added an answer on May 27, 2026 at 10:54 am

    This is a little bit weird but I’m going to answer my own question. I just figured out how to make my example above work.

    Here it goes the solution. Everytime we need to serialize a class that inherits attributes from another class we need to use the macro:

    BOOST_CLASS_EXPORT(T)
    

    According to the boost serialization doc

    BOOST_CLASS_EXPORT in the same source module that includes any of the archive class headers will instantiate code required to serialize polymorphic pointers of the indicated type to the all those archive classes. If no archive class headers are included, then no code will be instantiated.

    Note that the implemenation of this functionality requires that the BOOST_CLASS_EXPORT macro appear after and the inclusion of any archive class headers for which code is to be instantiated.

    So in my case my main.cpp file is now:

    #include <fstream>
    
    #include <boost\archive\text_iarchive.hpp>
    #include <boost\archive\text_oarchive.hpp>
    #include <boost\serialization\export.hpp>
    
    #include "Coordinate.h"
    #include "Move.h"
    #include "MoveNormal.h"
    BOOST_CLASS_EXPORT(MoveNormal)
    
    int main(int argc, char *argv[]) {
        Coordinate *c = new Coordinate(150);
        Move *m = new MoveNormal(c);
        std::cout << "m.getDestination().get(): " << m->getDestination()->get() << std::endl;
        {
            std::ofstream ofs("f.txt");
            boost::archive::text_oarchive oa(ofs);
            oa << m;
        }
    
        Move *n;
        {
            std::ifstream ifs("f.txt");
            boost::archive::text_iarchive ia(ifs);
            ia >> n;
        }
        std::cout << "n.getDestination().get(): " << n->getDestination()->get() << std::endl;
        return 0;
    }
    

    Just make sure that you include all the boost archives you need before you use the export MACRO.

    To finish my project besides the serialization I need to write them to a tcp socket using boost::asio.

    So let’s assume that I have a connection header like this one and that now I have another class called MoveJump defined in my MoveJump.h

    #include <boost\archive\text_iarchive.hpp>
    #include <boost\archive\text_oarchive.hpp>
    
    #include "Coordinate.h"
    #include "Move.h"
    
    class MoveJump : public Move {
    public:
        MoveJump() {}
        MoveJump(Coordinate *c) { this->destinatio = c; }
        ~MoveJump() {}
        virtual bool isJump(void);
    private:
        friend class boost::serialization::access;
        template<typename Archive>
        void serializize(Archive &ar, const unsigned int version) {
            ar & boost::serialization::base_object<Move>(*this);
        }
    };
    

    Now to serialize these structures my main look like this

    #include <boost\archive\text_iarchive.hpp>
    #include <boost\archive\text_oarchive.hpp>
    #include <boost\serialization\export.hpp>
    
    #include <fstream>
    
    #include "Coordinate.h"
    #include "Move.h"
    // And now we register all the possible Moves
    #include "MoveNormal.h"
    BOOST_CLASS_EXPORT(MoveNormal)
    #include "MoveJump.h"
    BOOST_CLASS_EXPORT(MoveJump)
    
    int main(int argc, char *argv[]) {
        Coordinate *c = new Coordinate(10);
        Move *m = new MoveNormal(c);
        Coordinate *d = new Coordinate(15);
        Move *j = new MoveJump(d);
        {
            std::ofstream ofs("m.txt");
            boost::archive::text_oarchive oa(ofs);
            oa << m;
        }
        {
            std::ofstream ofs("j.txt");
            boost::archive::text_oarchive oa(ofs);
            oa << j;
        }
    }
    

    The trick is to register the classes that will be serialized when we have the pointer to the base class.

    If inside my Move.h I have more pointers to other base classes, which I do in my project, we need to include in the main all the headers and register all the possible classes that expand the base class.

    I hope this helps someone who might have similar problems in the future.

    Feel free to present new possible solutions.

    Thanks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.