我试图序列化我使用我定义的Pair类的InvertedIndex类,它也是可序列化的。
这是我的invertedIndex代码:
#ifndef INVERTED_IND
#define INVERTED_IND
#include <map>
#include <string>
#include <vector>
#include "Pair.h"
#include <boost/serialization/access.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
class invertedIndex
{
friend class boost::serialization::access;
public:
std::map<std::string,std::vector<Pair> > m; //term: (docid,freq)
void updateIndex (std::map<std::string,int>,int);
//Updates inverted index given a map(term:freq),int id
std::map<int,int> queryIndex(std::vector<std::string> terms);
//queries the inverted index given a list of terms, and returns a map(docID:score)
invertedIndex chunkIndex(std::vector<std::string> terms);
//creates an II object with only those terms that are contained in the terms argument
std::string toString();
//toString method for debugging
template<class Archive>
void serialize(Archive&ar,const unsigned int version)
{
ar & m;
}
private:
void addTerm(std::string term,std::vector<Pair>);
//add partial result to this inverted index
};
#endif
这是Pair头文件:
#ifndef PAIR_T
#define PAIR_T
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <string>
#include <iostream>
#include <sstream>
class Pair
{
friend class boost::serialization::access;
public:
int docID;
int freq;
template<class Archive>
void serialize(Archive &ar,const unsigned int version)
{
ar & docID;
ar & freq;
}
Pair(int x, int y);
Pair(int x);
std::string toString();
};
#endif
当我尝试编写一个main方法序列化上面的代码时,编译器发出大量错误,说boost / serialization / access.hpp:109 invertedIndex没有名为serialize的成员。
所以我的问题是我不允许直接序列化倒排索引的地图数据成员吗?这是因为它是一个复杂的数据结构,里面有Vector<Pair>
吗?
编辑:这里是编译命令和输出
g++ -Wall -g -DBOOST_SYSTEM_NO_DEPRECATED iiTest.cpp Pair.cpp invertedIndex.cpp -o iiTest -lboost_serialization -lboost_system-mt
In file included from Pair.h:4,
from invertedIndex.h:6,
from iiTest.cpp:1:
/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 = invertedIndex]':
/usr/include/boost/serialization/serialization.hpp:74: instantiated from 'void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = invertedIndex]'
/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 = invertedIndex]'
/usr/include/boost/archive/detail/oserializer.hpp:139: 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 = invertedIndex]'
/usr/include/boost/archive/detail/oserializer.hpp:96: instantiated from 'boost::archive::detail::oserializer<boost::archive::text_oarchive, invertedIndex>'
/usr/include/boost/archive/detail/oserializer.hpp:235: instantiated from 'static void boost::archive::detail::save_non_pointer_type<Archive>::save_standard::invoke(Archive&, const T&) [with T = invertedIndex, Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/detail/oserializer.hpp:290: instantiated from 'static void boost::archive::detail::save_non_pointer_type<Archive>::invoke(Archive&, const T&) [with T = invertedIndex, Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/detail/oserializer.hpp:296: instantiated from 'static void boost::archive::detail::save_non_pointer_type<Archive>::invoke(Archive&, T&) [with T = invertedIndex, Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/detail/oserializer.hpp:507: instantiated from 'void boost::archive::save(Archive&, T&) [with Archive = boost::archive::text_oarchive, T = invertedIndex]'
/usr/include/boost/archive/detail/common_oarchive.hpp:62: instantiated from 'void boost::archive::detail::common_oarchive<Archive>::save_override(T&, int) [with T = invertedIndex, Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/basic_text_oarchive.hpp:75: instantiated from 'void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = invertedIndex, Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: instantiated from 'Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = invertedIndex, Archive = boost::archive::text_oarchive]'
iiTest.cpp:34: instantiated from here
/usr/include/boost/serialization/access.hpp:109: error: 'class invertedIndex' has no member named 'serialize'
/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_iarchive, T = invertedIndex]':
/usr/include/boost/serialization/serialization.hpp:74: instantiated from 'void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = invertedIndex]'
/usr/include/boost/serialization/serialization.hpp:133: instantiated from 'void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = invertedIndex]'
/usr/include/boost/archive/detail/iserializer.hpp:173: instantiated from 'void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive, T = invertedIndex]'
/usr/include/boost/archive/detail/iserializer.hpp:115: instantiated from 'boost::archive::detail::iserializer<boost::archive::text_iarchive, invertedIndex>'
/usr/include/boost/archive/detail/iserializer.hpp:363: instantiated from 'static void boost::archive::detail::load_non_pointer_type<Archive>::load_standard::invoke(Archive&, const T&) [with T = invertedIndex, Archive = boost::archive::text_iarchive]'
/usr/include/boost/archive/detail/iserializer.hpp:415: instantiated from 'static void boost::archive::detail::load_non_pointer_type<Archive>::invoke(Archive&, T&) [with T = invertedIndex, Archive = boost::archive::text_iarchive]'
/usr/include/boost/archive/detail/iserializer.hpp:554: instantiated from 'void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive, T = invertedIndex]'
/usr/include/boost/archive/detail/common_iarchive.hpp:61: instantiated from 'void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = invertedIndex, Archive = boost::archive::text_iarchive]'
/usr/include/boost/archive/basic_text_iarchive.hpp:62: instantiated from 'void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = invertedIndex, Archive = boost::archive::text_iarchive]'
/usr/include/boost/archive/text_iarchive.hpp:66: instantiated from 'void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = invertedIndex, Archive = boost::archive::text_iarchive]'
/usr/include/boost/archive/detail/interface_iarchive.hpp:61: instantiated from 'Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = invertedIndex, Archive = boost::archive::text_iarchive]'
iiTest.cpp:37: instantiated from here
/usr/include/boost/serialization/access.hpp:109: error: 'class invertedIndex' has no member named 'serialize'
主要方法的相关部分(iiTest.cpp):
stringstream ss;
boost::archive::text_oarchive oarch(ss);
oarch << ii; //ii is a previously declared invertedIndex
boost::archive::text_iarchive iarch(ss);
invertedIndex new_map;
iarch >> new_map;
std::cout << new_map.toString() << std::endl;
答案 0 :(得分:0)
我没有看到您的代码存在问题。
也许你可以用不同的方式解决你的问题:
<强> Live On Coliru 强>
#include <boost/serialization/access.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <sstream>
class Pair {
friend class boost::serialization::access;
public:
int docID;
int freq;
template <class Archive> void serialize(Archive &ar, const unsigned int version) {
ar &docID;
ar &freq;
}
Pair(int x=0, int y=0) : docID(x), freq(y) {}
std::string toString();
};
class invertedIndex {
friend class boost::serialization::access;
public:
std::map<std::string, std::vector<Pair> > m; // term: (docid,freq)
void updateIndex(std::map<std::string, int>, int); // Updates inverted index given a map(term:freq),int id
std::map<int, int> queryIndex(std::vector<std::string> terms); // queries the inverted index given a list of terms, and returns a map(docID:score)
invertedIndex chunkIndex(std::vector<std::string> terms); // creates an II object with only those terms that are contained in the terms argument
std::string toString(); // toString method for debugging
template <class Archive> void serialize(Archive &ar, const unsigned int version) { ar &m; }
private:
void addTerm(std::string term, std::vector<Pair>);
// add partial result to this inverted index
};
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
int main() {
std::stringstream ss;
{
boost::archive::text_oarchive oa(ss);
invertedIndex ii;
oa << ii;
}
std::cout << ss.str() << "\n";
{
boost::archive::text_iarchive ia(ss);
invertedIndex ii;
ia >> ii;
}
}
成功运行,打印
22 serialization::archive 13 0 0 0 0 0 0