Boost c++ serialization problem



  • Hallo all,
    I have just install boost 1.38 framework and compiled following code in VC-2003
    using the boost_serialization-vc71-mt-1_38.lib und dll

    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    
    using namespace std;
    
    void save() 
    { 
      std::ofstream file("archiv.txt"); 
      boost::archive::text_oarchive oa(file); 
      int i = 7; 
      oa << i; 
    } 
    
    void load() 
    { 
      std::ifstream file("archiv.txt"); 
      boost::archive::text_iarchive ia(file); 
      int i = 0; 
      ia >> i; 
      std::cout << i << std::endl; 
    } 
    
    //==========================================================
    /// Main function
    //==========================================================
    void main(int argc, char* argv[])
    {
    	save();
    	load();
    	int i;
    	cin >>i; 
    	getchar();
    }
    

    Always "archiv.txt" file only stores "22 serialization::archive 5"
    instead of "22 serialization::archive 5 1"
    Where did i mistake? 😮 any hint would be helpful. 🙂

    //Reactivated BBCode and changed tags to cpp-tags for C++ syntax highlighting



  • KnightRider schrieb:

    ...

    Should work perfect. Serializes 7 to the archive in save und loads 7 back from the archive in load .



  • It should be working but its not , is there any Flags or namespace to include in VC project??



  • KnightRider schrieb:

    It should be working but its not , is there any Flags or namespace to include in VC project??

    No.
    I've just tried it and it works perfectly for me. Are there any exceptions thrown by the serialization calls?



  • Ok- Now i have tried with static library , its working

    Now i am trying to serialize my class but it show memory error:
    Microsoft C++ exception: __non_rtti_object @ 0x0012f0e0.

    Here is my code:

    /////######  person.h  ######
    #include <boost/archive/text_oarchive.hpp> 
    #include <boost/archive/text_iarchive.hpp> 
    #include <iostream> 
    #include <sstream> 
    #include <stdio.h>
    
    class person 
    { 
    public: 
      person();
      person(int age);
       int getAge() ;
    
    private: 
      friend class boost::serialization::access; 
    
      template <typename Archive> 
      void serialize(Archive &ar, const unsigned int version) 
      { 
        ar & age_; 
      } 
      int age_; 
    }; 
    
    ///#####  person.cpp ######
    #include "person.h"
    person::person(){}
    person::person(int age):age_(age){}
    int person::getAge() {   return age_; } 
    
    ///###### main #######
    #include <boost/archive/text_oarchive.hpp> 
    #include <boost/archive/text_iarchive.hpp> 
    #include <iostream> 
    #include <sstream> 
    #include <stdio.h>
    #include "person.h"
    #include <fstream>
    
    void saveData() 
    { 
       std::ofstream file("archiv.txt"); 
       boost::archive::text_oarchive oa( file ); 
       person p(31); 
    
      oa << p;  ///here VC throws exception
    } 
    
    void loadData() 
    { 
      std::ifstream file("archiv.txt"); 
      boost::archive::text_iarchive ia( file ); 
      person p; 
      ia >> p; 
      std::cout << p.getAge() << std::endl; 
    } 
    
    int main() 
    { 
       saveData(); 
       loadData(); 
       getchar();
    }
    

    Any hint would be helpful
    Thanks in advance 🙂

    //Changed code tags to cpp tags



  • I have just installed the boost library and include it in my project.
    Where i am making the mistake? Could someone write me the VS Project settings.

    By executing the above code Visual studio show the error message dialog

    void *malloc() - Get a block of memory from the debug heap
    *
    *Purpose:
    *       Allocate of block of memory of at least size bytes from the heap and
    *       return a pointer to it.
    *
    *       Allocates 'normal' memory block.
    *
    *Entry:
    *       size_t          nSize       - size of block requested
    *
    *Exit:
    *       Success:  Pointer to memory block
    *       Failure:  NULL (or some error value)
    *
    *Exceptions:
    *
    *******************************************************************************/
    
    _CRTIMP void * __cdecl malloc (
            size_t nSize
    

Anmelden zum Antworten