Thread: Saving by pointer

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Saving by pointer

    Hi, I'am starting to learn serialization and I have proably quite simple problem but I don'tknow how to save object by pointer, with using boost serialization. I have such code

    Code:
    class might
    {
    private:
        string name;
        string descripction;
        int level;
        int maxlevel;
        int *mana;
        int price;
        int upgrade;
        int upgradecost;
        int manaup;
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & Ar, const int version){
            Ar & name;
            Ar & descripction;
            Ar & level;
            Ar & maxlevel;
            Ar & mana;
            Ar & price;
            Ar & upgrade;
            Ar & upgradecost;
            Ar & manaup;
        }
    
    public:
        might() {}
        might(string id, string about, int magic, int endlevel, int cost, int upg, int upcost, int manacost);
        friend std::ostream& operator<<(std::ostream& os, const moc& m);
         void save(might &m, string name);
         void load(might &m, string name);
         ~might();
    };
    
    //save, load methods
    void might::save(might &m, string name){
        std::ofstream ofs(name);
        boost::archive::text_oarchive oa(ofs);
        oa << m;
    }
    
    void might::load(might &m, string name){
        std::ifstream ifs(name);
        boost::archive::text_iarchive ia(ifs);
        ia >> m;
    }
    //code in main()
    string name;
    cin >> name;               
    might *something = new might();
    something->save(*something,name);
    When I'm trying to compile this I'm getting "left of '.serialize' must have class/struction/union "
    I'm I thinking wrong somewhere how can I fix it?
    Last edited by Sabriael; 03-10-2013 at 03:37 PM. Reason: code was edited bad

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    And where is this error at?

    Soma

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int *mana;
    Why is this a pointer, and not say std::vector<int> mana ?

    Further, which member of the class tells you how many elements are allocated to this pointer?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I don't know what you expect lines 16 to 24 to do, but the bitwise-and operation is not normally expected to have side-effects.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMalc View Post
    I don't know what you expect lines 16 to 24 to do, but the bitwise-and operation is not normally expected to have side-effects.
    That's how you serialize stuff with Boost.Serialize.
    See: http://www.boost.org/doc/libs/1_53_0.../tutorial.html
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Quote Originally Posted by Salem View Post
    >int *mana;
    Why is this a pointer, and not say std::vector<int>mana ?
    Thanks, this is the problem if I delete Ar& mana, this code saves everything without a problem (expectof course value of *mana) :

    Code:
    string name;
    cin >> name; 
     might *something = new might("MoreTries","You got more tries",12,10,50,100,30,4);
    something->save(*something,name);
    So the problem is serializing pointer.

    Quote Originally Posted by Salem View Post
    Further,which member of the class tells you how many elements are allocatedto this pointer?
    I've defined it in constructor,should have gived all the methods, here there are:

    Code:
    might::might(string id, string about, int magic, int endlevel, int cost, int upg, int upcost, int manacost){
    name=id;
    descripction=about;
    level=1;
    maxlevel=endlevel;
    price=cost;
    upgrade=upg;
    upgradecost=upcost;
    manaup=manacost;
    mana= new int[maxlevel];
    for(int a=0; a<maxlevel;a++){
    mana[a]=magic+manaup*a;
    }
    }
    
    might::~might(){
    delete[] mana;
    }
    
    ostream& operator<<(ostream& os,const might& m){
    os << m.name << " " <<m.descripction << " " << m.mana[m.level -1] <<" " << m.level << " " <<m.maxlevel << " " << m.price << " "<< m.upgrade << " " << m.upgradecost <<" " << m.manaup << endl;
    return os;
    }
    So when I'm trying to save this with Ar & mana (same for *mana or vector<int> mana) it gives me error "left of '.serialize'must have class/struction/union ".

    In tutorial there such example:
    Code:
    class bus_route
    {
    friend class boost::serialization::access;
    bus_stop * stops[10];
    template<classArchive>
    void serialize(Archive & ar, const unsigned intversion)
    {
    ar & stops;
    }
    and I don't see any reson to serialize pointers in a special way, am I missing something, why its not working?

    ps.
    Quote Originally Posted by Salem View Post
    > int*mana;
    Why is this a pointer, and not say std::vector<int>mana ?
    If I normaly want to have an array in class should I use STL container rather than pointer?
    Last edited by Sabriael; 03-11-2013 at 03:28 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't serialize a pointer: it's ambiguous what a pointer points to. It can point to some random place in memory, to some single element T or some array.
    Get rid of your pointer and use std::vector.
    The save and load functions seem pretty useless, too. Save should probably take a stream as parameter and serialize itself (ie, *this). Load should probably be a constructor, or it should serialize into itself (ie, *this).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saving struct with pointer to disk seg faults
    By 9three in forum C Programming
    Replies: 11
    Last Post: 03-29-2012, 01:13 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  4. Saving
    By RPG'er in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2002, 08:50 PM
  5. Saving
    By PJYelton in forum Linux Programming
    Replies: 3
    Last Post: 12-03-2002, 07:22 PM

Tags for this Thread