提升序列化,类包括指针

时间:2016-03-09 07:39:24

标签: c++ pointers serialization boost

我目前正致力于提升序列化,而且我进入了一个我无法前进的时刻。我有一个类Order,它包含Table和Waiter类的指针。我想补充说,所有其他类的序列化工作完全正常,它只是产生问题的顺序。当我尝试在我的主要序列化Order时,我获得豁免: ConsoleApplication1.exe中0x77A03FC8处的未处理异常:Microsoft C ++异常:内存位置0x0081C42C处的boost :: archive :: archive_exception。 和  + e {m_buffer = 0x0081c6f4"未注册的类 - 派生类未注册或导出" code = unregistered_class(2)} const boost :: archive :: archive_exception& 您是否知道如何在序列化调用中使此连接正常工作?

Order.h
#pragma once
#include <vector>
#include <string>
#include <iostream>
#include <ctime>    
#include "Meal.h"
#include "Table.h"
#include "Waiter.h"
using namespace std;
enum Status { ASSIGNED, PROCESSED, CONFIRMED, SUBMITTED, BEING_PREPARED, READY, SERVED, CANCELED, PAID, FINALIZED };


class Order
{
private:
friend std::ostream & operator<<(std::ostream &os, const Order  &tb);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */) {
    ar & ID & TimeAndDate & status & table & waiter & meals;
};
protected:


string TimeAndDate;
Status status;
Table * table;
Waiter * waiter;
string ID;

public:
typedef Meal * Meal_Pointer;
list<Meal_Pointer> meals;
Order(string,Table*,Waiter*);

Order():table(new Table),waiter(new Waiter) {};
string CurrentDateAndTime();
void UpdateStatus(Status);
void AssignWaiter(Waiter*);
void AssignTable(Table*);
void AddItem(Meal*);
void append(Meal *_bs)
{
    meals.insert(meals.end(), _bs);
}

};

Order.cpp

#include "Order.h"


Order::Order(string id, Table* tab, Waiter* wait)
:ID(move(id)), table(tab), waiter(wait), status(ASSIGNED), 







std::ostream & operator<<(std::ostream &os, const Order &tb)
{
//std::list<Meal *>::const_iterator it;
// note: we're displaying the pointer to permit verification
// that duplicated pointers are properly restored.

return os << ' ' << tb.ID << ' ' << tb.TimeAndDate << ' ' << tb.status << '      ' << tb.table << ' ' << tb.waiter << ' ' ;

/*for (it = tb.meals.begin(); it != tb.meals.end(); it++) {
    os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
}*/
}

template void Order::serialize<boost::archive::text_iarchive>(
boost::archive::text_iarchive & ar,
const unsigned int file_version
);
template void Order::serialize<boost::archive::text_oarchive>(
boost::archive::text_oarchive & ar,
const unsigned int file_ver

功能

void save_order(const Order &s, const char * filename) {
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
int main ()
{
filename = "order.txt";

save_order(myOrder, filename.c_str());
Order neworder;
restore_order(neworder, filename.c_str());}

1 个答案:

答案 0 :(得分:0)

您不会显示相关代码。

你所展示的代码是混乱而破碎的。

我已经修好了它以便编译:

<强> Live On Coliru

显然,没问题。

// Order.h
//#pragma once
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <ctime>
#include <fstream>
#include <iostream>

using namespace std;

struct Table {};
struct Waiter {};
struct Meal {};

namespace boost { namespace serialization {
    template <typename Ar> void serialize(Ar&, Table&, unsigned) { }
    template <typename Ar> void serialize(Ar&, Waiter&, unsigned) { }
    template <typename Ar> void serialize(Ar&, Meal&, unsigned) { }
} }

enum Status { ASSIGNED, PROCESSED, CONFIRMED, SUBMITTED, BEING_PREPARED, READY, SERVED, CANCELED, PAID, FINALIZED };

class Order {
  private:
    friend std::ostream &operator<<(std::ostream &os, const Order &tb);
    friend class boost::serialization::access;
    template <class Archive> void serialize(Archive &ar, const unsigned int /* file_version */) {
        ar &ID &TimeAndDate &status &table &waiter &meals;
    }

  protected:
    string TimeAndDate;
    string ID;
    Table *table;
    Waiter *waiter;
    Status status;
    typedef Meal *Meal_Pointer;
    list<Meal_Pointer> meals;

  public:
    Order(string, Table *, Waiter *);
    Order() : table(new Table), waiter(new Waiter){};

    string CurrentDateAndTime();
    void UpdateStatus(Status);
    void AssignWaiter(Waiter *);
    void AssignTable(Table *);
    void AddItem(Meal *);
    void append(Meal *_bs) { meals.insert(meals.end(), _bs); }
};

// Order.cpp

//#include "Order.h"

Order::Order(string id, Table *tab, Waiter *wait) : ID(move(id)), table(tab), waiter(wait), status(ASSIGNED) {}
std::ostream &operator<<(std::ostream &os, const Order &tb) {
    return os << ' ' << tb.ID << ' ' << tb.TimeAndDate << ' ' << tb.status << ' ' << tb.table << ' ' << tb.waiter
              << ' ';

    /*for (it = tb.meals.begin(); it != tb.meals.end(); it++) {
        os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
    }*/
}

template void Order::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive &ar, const unsigned int file_version);
template void Order::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive &ar, const unsigned int file_ver);

void save_order(const Order &s, const char *filename) {
    // make an archive
    std::ofstream ofs(filename);
    boost::archive::text_oarchive oa(ofs);
    oa << s;
}

void restore_order(Order &s, const char *filename) {
    std::ifstream ifs(filename);
    boost::archive::text_iarchive ia(ifs);
    ia >> s;
}

int main() {
    std::string const filename = "order.txt";

    {
        Order myOrder;
        save_order(myOrder, filename.c_str());
    }

    std::cout << std::ifstream(filename).rdbuf() << "\n";

    {
        Order neworder;
        restore_order(neworder, filename.c_str());
    }
}

提示

您似乎正在做的是序列化类的多态实例(例如,从Table,Waiter或Meal派生)。在这种情况下,您需要注册派生类并正确序列化基础: