gpt4 book ai didi

c++ - boost : problems with placement new 中的 load_construct_data

转载 作者:行者123 更新时间:2023-11-28 03:20:49 24 4
gpt4 key购买 nike

我尝试在 boost 的帮助下使用非默认构造函数序列化派生指针类。

在编译过程中出现错误:

Derived.h: In function ‘void boost::serialization::load_construct_data(Archive&, const A::Derived*, unsigned int)’:

in Derived.h: error: no matching function for call to ‘operator new(long unsigned int, const A::Derived*&)

我包括了 <new>Derived.h ,但我有一种感觉,我忘了做某事。这是我对代码的粗略估计。

我有一个带有虚函数和非默认构造函数的基类(在 Base.h 中)

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
namespace A{

class Base

{

public:

int getID(){return ID};

//non default constructor
Base(param1, param2, param3):ID(param1+param2), BaseFlag(param3) {};
Base(param1, param3):ID(param1), BaseFlag(param3) {};

//some virtual functions
virtual void Foo1();
virtual void Foo2();
...

private:

int ID;
bool BaseFlag;
...

//void serialize function
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{

}

};

}
//end of namespace A
//implementation is in another file - exporting key
BOOST_CLASS_EXPORT_KEY(Base)

有一个派生类(在Derived.h中)

#include <boost/serialization/base_object.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <new>
#include "Base.h"

namespace A{

//derived class
class Derived: public Base
{
public:
//non default constructor
Derived(param3):Base(param3, false);
...
private:
friend class boost::serialization::access;
template<class Archive>

// serialize base class information
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<Base>(*this);
}

//prototype of save_construct_data for non-default constructor
template<class Archive> friend
void boost::serialization::save_construct_data(Archive & ar,
const Derived * t, const unsigned int file_version);

//prototype of load_construct_data for non-default constructor
template<class Archive> friend
void boost::serialization::load_construct_data(Archive & ar,
const Derived * t, const unsigned int file_version);
};
}
//end of namespace A

//export derived class
BOOST_CLASS_EXPORT_KEY(Derived)

//describe save_construct_data
namespace boost {
namespace serialization {
template<class Archive>
inline void save_construct_data(Archive & ar, const A::Derived * t, const unsigned int file_version)
{
// save data required to construct instance
ar << t->ID;

}

template<class Archive>
inline void load_construct_data(Archive & ar, const A::Derived * t, const unsigned int file_version)
{
int ID;
// load data required to construct instance
ar >> ID;
::new(t) A::Derived(ID);

}


}
}

在 main.cpp 的某处我想保存和加载派生类。所以,我在开头提到的编译错误阻止了我继续。

有什么提示我遗漏了什么吗?

最佳答案

我相信编译器在提示,因为您正试图在内存中构造一个标记为 const 的 Derived。在此声明中:

template<class Archive> friend 
void boost::serialization::load_construct_data(Archive & ar,
const Derived * t, const unsigned int file_version);

我想你想要Derived *t,而不是const Derived *t

boost docs函数签名中也没有const:

template<class Archive>
inline void load_construct_data(
Archive & ar, my_class * t, const unsigned int file_version
){
// retrieve data from archive required to construct new instance
int attribute;
ar >> attribute;
// invoke inplace constructor to initialize instance of my_class
::new(t)my_class(attribute);
}

关于c++ - boost : problems with placement new 中的 load_construct_data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15477464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com