Zobrazit příspěvky

Tato sekce Vám umožňuje zobrazit všechny příspěvky tohoto uživatele. Prosím uvědomte si, že můžete vidět příspěvky pouze z oblastí Vám přístupných.


Témata - Xgamer

Stran: [1]
1
Vývoj / Serializácia a deserializácia C++
« kdy: 30. 04. 2011, 18:02:43 »
Zdravím, mam ulohu do školy na c++, v ktorom mam spravit serializáciu a deserializáciu objektu. Kedže som to nejako nevedel spraviť v tom mojom projekte tak som si vymyslel niečo jednoduchšie aby som to pochopil ale aj tak som nepochodil:)

Tu je kod:
Kód: [Vybrat]
#include <iostream>
#include <fstream>

using namespace std;

class Car
{
   public:
        Car(){};
        Car(int w, int s, int p)
        {
            this->Weight = w;
            this->MaxSpeed = s;
            this->Price = p;
        }
        void setWeight(int w){ this->Weight = w;}
        void setMaxSpeed(int s) {this->MaxSpeed = s;}
        void setPrice(int p){this->Price = p;}

        inline int getWeight() const {return Weight;}
        inline int getMaxSpeed() const {return MaxSpeed;}
        inline int getPrice() const {return Price;}

         friend ostream& operator<<(ostream &os,Car &c);
    private:
        int Weight;
        int MaxSpeed;
        int Price;
};

ostream& operator<<(ostream &os,Car &c)
{
    return(os << c.Weight << "  " << c.MaxSpeed << "  " << c.Price << endl);
}

class CarTrain
{
    public:
        CarTrain(){};
        CarTrain(int s){this->size = s;this->carstore = new Car[size];}
        void loadCar(int i,Car c){this->carstore[i] = c;}
        Car getCar(int i){return this->carstore[i];}
        int getSize(){return this->size;}
        friend ostream& operator<<(ostream &os,Car &c);
        void serialize(ostream& os)
         {
             Car tmp;
            for(int i=0; i <this->size; i++)
            {
                tmp =  this->carstore[i];
                os << tmp;
            }
         }
        CarTrain unserialize(ifstream& in)
        {
            CarTrain tmp; Car c;
            int w,s,p;
            tmp.size = this->size;
            for(int i=0; i< this->size;i++)
            {
                in>>w;
                in>>s;
                in>>p;

                c.setWeight(w);
                c.setMaxSpeed(s);
                c.setPrice(p);
                tmp.loadCar(i,c);
            }
            return tmp;
        }
    private:
        Car *carstore;
        int size;
};

ostream& operator<<(ostream &os,CarTrain &ct)
{
    Car tmp;
    for(int i=0; i < ct.getSize(); i++)
    {
        tmp =  ct.getCar(i);
        os << tmp;
    }
    return os;
}

int main()
{
    cout << "Serializacia a deserializacia" << endl;

    Car c1(1000,150,8000);
    Car c2(1200,190,9000);
    Car c3(1400,220,120000);

    CarTrain ct(10);
    ct.loadCar(0,c1);
    ct.loadCar(1,c2);
    ct.loadCar(2,c3);

    cout<<ct<<endl;
    char *p = "ser.txt";
    ofstream o;
    o.open(p,ios::out);
    ct.serialize(o);
    o.close();

    ifstream in;
    in.open(p,ios::in);
    CarTrain ct2 = ct.unserialize(in);
    in.close();


    return 0;
}


Problem je to že takto tá deserializácia nefunguje. Možno to bude aj tým že som asi uplne nepochopil pointu toho. A hlavne mam vyriešiť aj verzovanie objektov ale to nejako už vobec neviem:) Zatial som skušal textovu serializáciu. Poprosil by som niekoho kto sa tomu rozumie aby mi to trochu objasnil, resp. čo robim zle ..:) Ďakujem

Stran: [1]