我在这个问题上已经失去了那么多时间。我有两个班,一个是一组卡
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <vector>
class Set {
private:
friend class boost::serialization::access;
std::string _name;
std::vector<Card*> _cards;
public:
template <class Archive>
void serialize(Archive & ar, const unsigned int version);
Set();
};
另一张卡本身:
class Card {
private:
friend class boost::serialization::access;
std::string _front;
std::string _back;
std::vector<CardReview> _reviews;
public:
template <class Archive>
void serialize( Archive & ar, const unsigned int version);
Card();
};
以下是我的序列化程序:
template <class Archive>
void Set::serialize ( Archive & ar, const unsigned int version )
{
ar & _cards; // If I comment this line, everything works !!
ar & _name;
}
template <class Archive>
void Card::serialize ( Archive & ar, const unsigned int version )
{
ar & _back;
ar & _front;
ar & _id;
ar & _reviews;
}
除非我对序列化名为_cards的卡片的行进行注释,否则会出现以下链接器错误:
[ 33%] Building CXX object CMakeFiles/supercards.dir/set.o
Linking CXX executable supercards
CMakeFiles/supercards.dir/set.o: In function `void boost::serialization::access::serialize<boost::archive::text_oarchive, Card>(boost::archive::text_oarchive&, Card&, unsigned int)':
set.cpp:(.text._ZN5boost13serialization6access9serializeINS_7archive13text_oarchiveE4CardEEvRT_RT0_j[_ZN5boost13serialization6access9serializeINS_7archive13text_oarchiveE4CardEEvRT_RT0_j]+0x1b): undefined reference to `void Card::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, unsigned int)'
CMakeFiles/supercards.dir/set.o: In function `void boost::serialization::access::serialize<boost::archive::text_iarchive, Card>(boost::archive::text_iarchive&, Card&, unsigned int)':
set.cpp:(.text._ZN5boost13serialization6access9serializeINS_7archive13text_iarchiveE4CardEEvRT_RT0_j[_ZN5boost13serialization6access9serializeINS_7archive13text_iarchiveE4CardEEvRT_RT0_j]+0x1b): undefined reference to `void Card::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive&, unsigned int)'
collect2: error: ld returned 1 exit status
make[2]: *** [supercards] Error 1
make[1]: *** [CMakeFiles/supercards.dir/all] Error 2
make: *** [all] Error 2
*** Failure: Exit code 2 ***
我真的不明白为什么会这样。如果我评论上面提到的行,没有编译时错误,序列化工作正常 - 但当然不包括_cards属性。所以这不是链接器错误,库正确链接。我花了3个小时试图找出代码中缺少的内容但没有成功。
任何想法都会受到赞赏,
谢谢!