8 Replies - 442 Views - Last Post: 26 April 2012 - 11:22 PM Rate Topic: -----

#1 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 511
  • Joined: 21-December 08

Boost deserialization error

Posted 26 April 2012 - 12:44 AM

Hello!
This is a question for those who use boost in C++.

I have the following code:

#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 
#include <boost/serialization/string.hpp> 
#include <iostream> 
#include <string> 
#include <fstream>

using namespace std;

class person
{
public:

	string _name;
	int _age;
	person (string name="Mike", int age=30):_name(name),_age(age){};

	friend class boost::serialization::access;
	
	template <typename Archive> 
	void serialize(Archive & ar, const unsigned int version)
    {
        ar & _name;
		ar & _age;
    }

};


void save() 
{ 
  ofstream file("archiv.cst"); 
  boost::archive::binary_oarchive oa(file); 


  for (int i=1; i<=25; i++)
  {

	  oa<<person("John",i);
  }

  file.close();
}; 

void load() 
{ 
  ifstream file("archiv.cst"); 
  boost::archive::binary_iarchive ia(file); 

  for (int i=1; i<=25; i++)
  {
      person p;
	  ia>>p;
	  cout << p._name<<" " <<p._age<<endl; 
  }

  file.close();
};


int main() 
{ 
	int i;
	cin>>i;

	while (i!=2)
	{
		if (i==0) save();
		if (i==1) load();
		cin>>i;
	}
} 


This code serializes 25 objects of type person. I also deserializes 25 objects of type person.My question to you is why this code does not work for more than 25 objects. I mean if I put 26 instead of 25 than i get a boost archive exception.

I just cannot figure out why.

Is This A Good Question/Topic? 0
  • +

Replies To: Boost deserialization error

#2 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 511
  • Joined: 21-December 08

Re: Boost deserialization error

Posted 26 April 2012 - 03:08 AM

Nobody knows?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5662
  • View blog
  • Posts: 22,505
  • Joined: 23-August 08

Re: Boost deserialization error

Posted 26 April 2012 - 03:22 AM

Your code does not compile for me:

g++ -Wall -pedantic -I/opt/local/include -oboostser boostser.cpp 
boostser.cpp: In function ‘void save()’:
boostser.cpp:39: error: no match for ‘operator<<’ in ‘oa << person(std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"John"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))), i)’
/opt/local/include/boost/archive/detail/interface_oarchive.hpp:62: note: candidates are: Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = person, Archive = boost::archive::binary_oarchive]


After fixing that, it's running fine for me.

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

#define COUNT 30

class person
{
public:

   string _name;
   int _age;
   person (string name="Mike", int age=30):_name(name),_age(age){};

   friend class boost::serialization::access;

   template <typename Archive>
   void serialize(Archive & ar, const unsigned int version)
    {
        ar & _name;
      ar & _age;
    }

};


void save()
{
  ofstream file("archiv.cst");
  boost::archive::binary_oarchive oa(file);


  for (int i=1; i<=COUNT; i++)
  {
      person p("John", i);
     oa<< p;
  }

  file.close();
}

void load()
{
  ifstream file("archiv.cst");
  boost::archive::binary_iarchive ia(file);

  for (int i=1; i<=COUNT; i++)
  {
      person p;
     ia>>p;
     cout << p._name<<" " <<p._age<<endl;
  }

  file.close();

}


int main()
{
   int i;
   cin>>i;

   while (i!=2)
   {
      if (i==0) save();
      if (i==1) load();
      cin>>i;
   }
}



g++ -Wall -pedantic -I/opt/local/include -o boostser boostser.cpp -L/opt/local/lib -lboost_serialization


./boostser 
0
1
John 1
John 2
John 3
John 4
John 5
John 6
John 7
John 8
John 9
John 10
John 11
John 12
John 13
John 14
John 15
John 16
John 17
John 18
John 19
John 20
John 21
John 22
John 23
John 24
John 25
John 26
John 27
John 28
John 29
John 30
2


This post has been edited by JackOfAllTrades: 26 April 2012 - 03:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 511
  • Joined: 21-December 08

Re: Boost deserialization error

Posted 26 April 2012 - 03:37 AM

Thank you, but how is it that is running fine to you and to me not?

what do you mean by this
g++ -Wall -pedantic -I/opt/local/include -o boostser boostser.cpp -L/opt/local/lib -lboost_serialization

??
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5662
  • View blog
  • Posts: 22,505
  • Joined: 23-August 08

Re: Boost deserialization error

Posted 26 April 2012 - 03:39 AM

I don't know why it's not working for you, but is for me (after I fixed the error).

And that's the command line I used to compile the program. And the code I used that worked.

Maybe you could provide exactly the steps to reproduce the error.
Was This Post Helpful? 0
  • +
  • -

#6 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 511
  • Joined: 21-December 08

Re: Boost deserialization error

Posted 26 April 2012 - 03:48 AM

View PostJackOfAllTrades, on 26 April 2012 - 03:39 AM, said:

Maybe you could provide exactly the steps to reproduce the error.


there are no steps. For me it works only if COUNT is <=25. Otherwise it gives me this error: Unhandled exception at 0x7c812afb in Boost_tests.exe: Microsoft C++ exception: boost::archive::archive_exception at memory location 0x0012e27c..

if I change to text archive and not binary than i don't have this issue.
I just cannot understand why is that!
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5662
  • View blog
  • Posts: 22,505
  • Joined: 23-August 08

Re: Boost deserialization error

Posted 26 April 2012 - 04:05 AM

If you take the code I created, and substitute it for yours, does the same thing happen?

Notice how I'm not using "MAGIC NUMBERS" like 25, instead opting for a #define COUNT.
Was This Post Helpful? 0
  • +
  • -

#8 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 511
  • Joined: 21-December 08

Re: Boost deserialization error

Posted 26 April 2012 - 04:12 AM

View PostJackOfAllTrades, on 26 April 2012 - 04:05 AM, said:

If you take the code I created, and substitute it for yours, does the same thing happen?

Notice how I'm not using "MAGIC NUMBERS" like 25, instead opting for a #define COUNT.


You're code does not work for me. I substituted it.
It gives me the error above.

This post has been edited by livium: 26 April 2012 - 04:13 AM

Was This Post Helpful? 0
  • +
  • -

#9 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 511
  • Joined: 21-December 08

Re: Boost deserialization error

Posted 26 April 2012 - 11:22 PM

Nobody knows?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1