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 6922557
In Process

The Archive Base Latest Questions

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

I’m trying to learn some basics of boost serialization. So I followed the tutorial

  • 0

I’m trying to learn some basics of boost serialization. So I followed the tutorial and created simple class A and class B and class C that has A a_; and B b_; in it as private members.

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

class A{
private:
        friend class boost::serialization::access;

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

        int a_;

public:
        A(){ std::cout << "A constructed" << std::endl; }
        A(int a): a_(a) { std::cout << "A constructed with 'a' ==" << a << std::endl; }
};
class B{
private:
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar & b_;
        }

        std::string b_;
public:
        B(){ std::cout << "B constructed" << std::endl; }
        B(std::string b): b_(b) { std::cout << "B constructed with 'b' ==" << b << std::endl; }
};

class C{
private:
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar & a_;
                ar & b_;
                ar & d_;
        }

        A a_;
        B b_;
        double d_;

public:
        C(){  std::cout << "C constructed" << std::endl; }
        C(int a, std::string b, double d ): a_(a), b_(b), d_(d) { std::cout << "C constructed with 'd' == " << d << std::endl; }
};

int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");

    // create class instance
    C c(15, "rock and roll", 25.8);

    // save data to archive
    {
            boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << c;
            // archive and stream closed when destructors are called
    }

    C c_recreated;
    {
            // create and open an archive for input
            std::ifstream ifs("filename");
            boost::archive::text_iarchive ia(ifs);
            // read class state from archive
            ia >> c_recreated;
            // archive and stream closed when destructors are called
    }

    std::cin.get();
}

In IDEone live it is here with all strange and scary compiler errors. While on my VS2010 I got only 2 identical errors:

Error   2   error C2248: 'C::serialize' : cannot access private member declared in class 'C'
Error   3   error C2248: 'C::serialize' : cannot access private member declared in class 'C'    

What have I done wrong, how can I make class C serializable after having class A and class B?

  • 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:25:19+00:00Added an answer on May 27, 2026 at 10:25 am

    You didn’t friend class boost::serialization::access; for B and C.

    • 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 am doing a simple coin flipping experiment for class that involves flipping a
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.