我是c ++的新手,刚刚发现了升级库。在我用来学习c ++的小程序中(我使用MSVC 2015),我想使用boost序列化来保存/加载我的播放器类。我在课堂上实现了所需的功能,如下所示:
Player.h
class Player
{
public:
Player();
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version);
int race; //For now im only attempting to serialize one variable to make it as simple as possible.
};
Player.cpp
using namespace std;
Player::Player()
{
race = -1; //Default value for race.
}
//Serialisation:
template<class Archive>
void Player::serialize(Archive & ar, const unsigned int version) {
ar & race;
}
...Other unnecessary functions not listed
Main.cpp的
void savePlayer() { //Called when player is to be saved.
string filename;
filename = p.getName() + "_" + getTime();
{
std::ofstream ofs(filename);
boost::archive::binary_oarchive oa(ofs);
oa << p; //Removing this line stops the error from occouring for some reason.
}
}
编译时出现以下错误:
Main.obj : error LNK2019: unresolved external symbol "private: void __thiscall Player::serialize<class boost::archive::binary_oarchive>(class boost::archive::binary_oarchive &,unsigned int)" (??$serialize@Vbinary_oarchive@archive@boost@@@Player@@AAEXAAVbinary_oarchive@archive@boost@@I@Z) referenced in function "public: static void __cdecl boost::serialization::access::serialize<class boost::archive::binary_oarchive,class Player>(class boost::archive::binary_oarchive &,class Player &,unsigned int)" (??$serialize@Vbinary_oarchive@archive@boost@@VPlayer@@@access@serialization@boost@@SAXAAVbinary_oarchive@archive@2@AAVPlayer@@I@Z)
1>E:\Users\James\Documents\Programming\Visual Studio 2015\Projects\Text Rpg v1.0\Debug\Text Rpg v1.0.exe : fatal error LNK1120: 1 unresolved externals
我已经在网上浏览了几个小时寻找解决方案,我能找到的最接近的是:http://boost.2283326.n4.nabble.com/Help-with-Visual-Studio-settings-for-Boost-Serialization-td3072458.html 找到这个问题的解决方案真的很好,因为在线没有明显的解决方案。
先谢谢 - Yepadee。