serialize explicit template instantiation

Hi, I want to use a non inline serialize method of a class. When instantiating it not explicitly the linker quits with undefined symbols. So I tried to instantiate it that way and it does not work anyway. Here is my code. Does anyone see what I've done wrong? I did also try to use export.hpp but it doesn't help too. Thank you very much, Jan. // Class.hpp #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> class CSerTest { std::string a_, b_, c_; friend class boost::serialization::access; template<class Archive> void serialize( Archive & ar, const unsigned int ); public: CSerTest(); }; BOOST_CLASS_VERSION(CSerTest, 1); // Class.cpp #include "Class.hpp" CSerTest::CSerTest() : a_("1"), b_("2"), c_("3") { } template<class Archive> void CSerTest::serialize( Archive & ar, const unsigned int ) { ar & BOOST_SERIALIZATION_NVP(a_) & BOOST_SERIALIZATION_NVP(b_) & BOOST_SERIALIZATION_NVP(c_); } // main.cpp #include <fstream> #include "Class.hpp" ///////////////////////// without effect //////////////////////////////////// template void CSerTest::serialize<boost::archive::xml_oarchive>(boost::archive::xml_oarchive&, unsigned int ); int main() { CSerTest instance; std::ofstream ofs( "out_file.txt" ); boost::archive::xml_oarchive oxa( ofs ); oxa << BOOST_SERIALIZATION_NVP(instance); } The linker (MS version 8) returns with: --- Creating library mytest.lib and object mytest.exp Main.obj : error LNK2019: unresolved external symbol "private: void __thiscall CSerTest::serialize<class boost::archive::xml_oarchive>(class boost::archive::xml_oarchive &,unsigned int)"(??$serialize@ Vxml_oarchive@archive@boost@@@CSerTest@@AAEXAAVxml_oarchive@archive@boost@@ I@Z) referenced in function "public: static void __cdeclboost::serialization::access::serialize<class boost::archive::xml_oarchive,class CSerTest>(class boost::archive::xml_oarchive &,class CSerTest &,unsigned int)"(??$serialize@ Vxml_oarchive@archive@boost@@VCSerTest@@@access@serialization@boost@@ SAXAAVxml_oarchive@archive@2@AAVCSerTest@@I@Z)mytest.exe : fatal error LNK1120: 1 unresolved externals

Try ///////////////////////// without effect //////////////////////////////////// template void CSerTest::serialize<boost::archive::xml_oarchive>(boost::archive::xml_oarchive&, const unsigned int ); // add const !!! Also a couple of unrelated suggestions: // Class.hpp // the following will bloat your code even whenever you use this header even when not actually using // xml_?archives - so better not to include it here: //#include <boost/archive/xml_oarchive .hpp> //#include <boost/archive/xml_iarchive.hpp> .... // main.cpp #include <fstream> #include "Class.hpp" #include <boost/archive/xml_oarchive .hpp> #include <boost/archive/xml_iarchive.hpp> ///////////////////////// without effect //////////////////////////////////// template void CSerTest::serialize<boost::archive::xml_oarchive>(boost::archive::xml_oarchive&, const unsigned int ); // "const" added!! to match template in declaration int main() { const CSerTest instance; // check rationale on this. std::ofstream ofs( "out_file.txt" ); boost::archive::xml_oarchive oxa( ofs ); oxa << BOOST_SERIALIZATION_NVP(instance); } **** this should work. Robert Ramey

Hi Robert, On 11/29/06, Robert Ramey <ramey@rrsd.com> wrote:
Try
///////////////////////// without effect //////////////////////////////////// template void
CSerTest::serialize<boost::archive::xml_oarchive>(boost::archive::xml_oarchive&, const unsigned int ); // add const !!!
Of course I did try this but the unresolved symbol is of type CSerTest::serialize<class boost::archive::xml_oarchive>(class boost::archive::xml_oarchive &,unsigned int) and so I tried it without const. But the error occurs further. // main.cpp
#include <fstream> #include "Class.hpp"
#include <boost/archive/xml_oarchive .hpp> #include <boost/archive/xml_iarchive.hpp>
///////////////////////// without effect //////////////////////////////////// template void
CSerTest::serialize<boost::archive::xml_oarchive>(boost::archive::xml_oarchive&, const unsigned int ); // "const" added!! to match template in declaration
int main() { const CSerTest instance; // check rationale on this.
std::ofstream ofs( "out_file.txt" ); boost::archive::xml_oarchive oxa( ofs );
oxa << BOOST_SERIALIZATION_NVP(instance); }
**** this should work.
I wish it would. Do you have another hint why the explicit instantiation doesn't work here? Thanks for your other suggestions. Cheers, jan

I just compiled, linked and ran the following program on my system: vc 7.1 Boost 1.35 - HEAD There was no need to explicitly instantiate anything. Robert Ramey #include <boost/serialization/string.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/version.hpp> class CSerTest { std::string a_, b_, c_; friend class boost::serialization::access; template<class Archive> void serialize( Archive & ar, const unsigned int ); public: CSerTest(); }; BOOST_CLASS_VERSION(CSerTest, 1); // Class.cpp #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> CSerTest::CSerTest() : a_("1"), b_("2"), c_("3") { } template<class Archive> void CSerTest::serialize( Archive & ar, const unsigned int ) { ar & BOOST_SERIALIZATION_NVP(a_) & BOOST_SERIALIZATION_NVP(b_) & BOOST_SERIALIZATION_NVP(c_); } // main.cpp #include <fstream> ///////////////////////// without effect //////////////////////////////////// //template void CSerTest::serialize<boost::archive::xml_oarchive>(boost::archive::xml_oarchive&, unsigned int ); int main() { const CSerTest instance; std::ofstream ofs( "out_file.txt" ); boost::archive::xml_oarchive oxa( ofs ); oxa << BOOST_SERIALIZATION_NVP(instance); }

Hi Robert, thank you for building the sample. I forgot to mention that I have to use the 1.33.1. Sorry for that. I'm working under a 'stable release' constraint. Cheers, Jan. On 11/29/06, Robert Ramey <ramey@rrsd.com> wrote:
I just compiled, linked and ran the following program on my system:
vc 7.1 Boost 1.35 - HEAD
There was no need to explicitly instantiate anything.
Robert Ramey
participants (2)
-
Jan Boehme
-
Robert Ramey