我一直在努力解决这个错误。 以下是我的代码片段。
//This is the header file
template<typename TElem>
class ArrayList {
public:
/** An accessible typedef for the elements in the array. */
typedef TElem Elem;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & ptr_;
ar & size_;
ar & cap_;
}
Elem *ptr_; // the stored or aliased array
index_t size_; // number of active objects
index_t cap_; // allocated size of the array; -1 if alias
};
template <typename TElem>
class gps_position
{
public:
typedef TElem Elem;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
private:
Elem degrees;
index_t minutes;
index_t seconds;
};
// This is the .cc file
#include <string>
#include <fstream>
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/serialization.hpp>
#include "arraylist.h"
int main() {
// create and open a character archive for output
std::ofstream ofs("filename");
// create class instance
// gps_position<int> g(35.65, 59, 24.567f);
gps_position<float> g;
// save data to archive
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
//oa << g;
// archive and stream closed when destructors are called
}
// ... some time later restore the class instance to its orginal state
/* gps_position<int> newg;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
// archive and stream closed when destructors are called
}*/
ArrayList<float> a1;
ArrayList<int> a2;
a1.Init(22);
a2.Init(21);
// a1.Resize(30);
// a1.Resize(12);
// a1.Resize(22);
// a2.Resize(22);
a1[21] = 99.0;
a1[20] = 88.0;
for (index_t i = 0; i < a1.size(); i++) {
a1[i] = i;
a1[i]++;
}
std::ofstream s("test.txt");
{
boost::archive::text_oarchive oa(s);
oa << a1;
}
return 0;
}
以下是我得到的编译错误。
In file included from /usr/include/boost/serialization/split_member.hpp:23,
from /usr/include/boost/serialization/nvp.hpp:33,
from /usr/include/boost/serialization/serialization.hpp:17,
from /usr/include/boost/archive/detail/oserializer.hpp:61,
from /usr/include/boost/archive/detail/interface_oarchive.hpp:24,
from /usr/include/boost/archive/detail/common_oarchive.hpp:20,
from /usr/include/boost/archive/basic_text_oarchive.hpp:32,
from /usr/include/boost/archive/text_oarchive.hpp:31,
from demo.cc:4:
/usr/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’:
/usr/include/boost/serialization/serialization.hpp:74: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’
/usr/include/boost/serialization/serialization.hpp:133: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’
/usr/include/boost/archive/detail/oserializer.hpp:140: instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = float]’
demo.cc:105: instantiated from here
/usr/include/boost/serialization/access.hpp:109: error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘float’
请帮帮我。
答案 0 :(得分:1)
您无法将原始指针序列化为浮点数,这是您在此处尝试执行的操作。事实上,当你尝试序列化a1时,你实际上并没有实际序列化数组 - 你只是尝试将指针序列化到它的头部。
我不知道你要完成什么,但是你能创建a1和a2原始浮点数组吗? Boost可以将这些本地序列化。
float a1[21];
a1[21] = 99.0;
for (index_t i = 0; i < 21; i++) {
a1[i] = i;
a1[i]++;
}
std::ofstream s("test.txt");
boost::archive::text_oarchive oa(s);
oa << a1;
更好的选择可能是将您的ArrayList定义更改为使用std::vector<Elem>
而不是原始Elem *
。 Boost :: Serialization也已经知道如何序列化所有STL容器。
有关详细信息,请参阅Boost serialization reference manual。