gpt4 book ai didi

c++ - 使用 Boost.Serialization 进行序列化

转载 作者:可可西里 更新时间:2023-11-01 18:09:25 25 4
gpt4 key购买 nike

我一直在尝试序列化,但每次我都遇到如下错误:-

error: 'class std::vector<int, std::allocator<int> >' has no member named 'serialize'

这是我的源代码和序列化方法,知道我正在使用 boost.serialize

template <class E, class T>
class heap{

vector<E> * hp;
int index;//index is pointing to first empty place after the last element
int maxsize;

T comp;//comparable object designed to compare the objects

private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<. Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{

ar & hp;
ar & index;
ar & maxsize;
ar & comp;
}

public:
//default empty constructor
heap(){

hp = new vector<E>(MAX);
index = 0;
maxsize = MAX;


}
.
.
.
.
.
.
}

然后我改变了这一行 ar & hp;进入

for(int i = 0; i < hp->size(); i++)
ar & hp->at(i);

我遇到了更严重的错误,比如

/../../../../boost/boost_1_48_0/boost/archive/text_oarchive.hpp:100: undefined reference to `boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::ostream&, unsigned int)'
debug/main.o: In function `~text_oarchive_impl':

Qt_4_8_0__4_8_0__Debug/../../../../boost/boost_1_48_0/boost/archive/text_oarchive.hpp:85: undefined reference to `boost::archive::basic_text_oprimitive<std::ostream>::~basic_text_oprimitive()'

Qt_4_8_0__4_8_0__Debug/../../../../boost/boost_1_48_0/boost/archive/text_oarchive.hpp:85: undefined reference to `boost::archive::basic_text_oprimitive<std::ostream>::~basic_text_oprimitive()'

最佳答案

  1. 包括<boost/serialization/vector.hpp> .
  2. 不分配vector动态地。
  3. 不要忘记链接到序列化,它不是只有 header 的库。

关于c++ - 使用 Boost.Serialization 进行序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9437109/

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