0
Severity  Code    Description Project File    Line    Suppression State
Error LNK2019 unresolved external symbol "private: void __thiscall
ABC::serialize<class boost::archive::xml_oarchive>(class
boost::archive::xml_oarchive &,unsigned int)"
(??$serialize@Vxml_oarchive@archive@boost@@@ABC@@AAEXAAVxml_oarchive@archive@boost@@I@Z) referenced in function "public: static void __cdecl
boost::serialization::access::serialize<class
boost::archive::xml_oarchive,class ABC>(class
boost::archive::xml_oarchive &,class ABC &,unsigned int)"
(??$serialize@Vxml_oarchive@archive@boost@@VABC@@@access@serialization@boost@@SAXAAVxml_oarchive@archive@2@AAVABC@@I@Z)   list-boost-xml  C:\Users\neha.gupta\source\repos\list-boost-xml\list-boost-xml.obj  1

I have created an application that serialize list of ABC into xml. I am using boost library of version 1_76_0 and visual studio 2019.While debugging I am getting above error. Please help me out why I am getting above error and how to resolve it. Please find the ABC class below:

class ABC
{
public:
    ABC();
    ABC(int x);
    int getData();
    void setdata(int);
    void display();
    
private:
    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive& ar, const unsigned int /*version*/);
    
    int data;
};
  • Is `ABC` a class template? Did you try implementing the class template's member functions in a `.cpp` file? – Ted Lyngmo Aug 31 '21 at 09:32
  • No ABC is not template class but serialize function is Template function – user1710979 Aug 31 '21 at 10:01
  • I have implemented the serialize function in .cpp file. – user1710979 Aug 31 '21 at 10:10
  • 1
    Then just write the implementation directly in the `class` definition you have in your question. `template void serialize(Archive& ar, const unsigned int) { ar & data; }` I guess. [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Ted Lyngmo Aug 31 '21 at 10:23
  • Thanks!! It worked. But, why I am getting error if writing implementation into .cpp file – user1710979 Aug 31 '21 at 10:32
  • Great! The explanation is that a function template is not a function. It must be instantiated first - and it won't be until used. It's not used when you compile your `.cpp` file (unless you have explicitly instantiated it - which is rare). There are more explanations in the link I provided above. – Ted Lyngmo Aug 31 '21 at 10:34

0 Answers0