提升序列化未定义的引用

时间:2016-05-24 22:43:04

标签: c++ serialization boost

#include <boost/serialization/vector.hpp>

template<class Archive>
void ScenarioResult::serialize(Archive & ar, const unsigned int version)
{
                ar & scenario; 
}

-lboost_serialization包含在链接器选项

在函数serialize<boost::mpi::packed_oarchive, ScenarioResult>': /people/v/boost_1_59_0/boost/serialization/access.hpp:116: undefined reference to void ScenarioResult :: serialize(boost :: mpi :: packed_oarchive&amp;,unsigned int)&#39; Integration.o:在函数serialize<boost::mpi::packed_iarchive, ScenarioResult>': /people/v/boost_1_59_0/boost/serialization/access.hpp:116: undefined reference to void ScenarioResult :: serialize(boost :: mpi :: packed_iarchive&amp;,unsigned int)&#39; collect2:错误:ld返回1退出状态

1 个答案:

答案 0 :(得分:3)

实际上,对这个问题进行编辑会很好,但是,我想我还有一个想法:看起来你正在尝试单独编译一个函数模板。

这意味着您正在执行以下操作:

g++ ScenarioResult.cpp -lboost_serialization

抛出错误。

ScenarioResult :: serialize是a template function,这意味着它会根据传递给的模板参数的类型进行实例化。因此,在编译时,编译器并不确切地知道类型,因此无法生成代码。

快速解决方案是to move the implementation of your serialize method into the header file of the class。或者,explicitly instansiate it in the cpp file如下:

void ScenarioResult::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive &ar, const unsigned int);