2

I do not know why I am getting this error when trying to compile. I have tried adding almost all of the types. I am trying to serialize a set of RegDoneEntry's . I know the call to serialize is not here but I can't get beyond just this. The below is updated to reflect recommendations.

The error I get now is

Severity Code Description Project File Line Suppression State Error LNK2001 unresolved external symbol "public: static class std::set,class std::allocator > RegDoneList::mylist" (?mylist@RegDoneList@@2V?$set@VRegDoneEntry@@U?$less@VRegDoneEntry@@@std@@V?$allocator@VRegDoneEntry@@@3@@std@@A) CerealTest CerealTest.obj 1

#include "stdafx.h"
#include "cereal/archives/binary.hpp"
#include "cereal/types/set.hpp"
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <stdio.h>
#include "test.h"

RegDoneList myRegDoneList;

int main()
{
    myRegDoneList = RegDoneList();

    myRegDoneList.mylist = std::set<RegDoneEntry>();
    std::ofstream ss("c:\\reg.bin", std::ofstream::binary | 
        std::ofstream::out | std::ofstream::trunc);

    cereal::BinaryOutputArchive oarchive(ss);
    oarchive(myRegDoneList); // Write the data to the archive

}

bool operator<(const RegDoneEntry& lhs, const RegDoneEntry& rhs)
{
    return lhs.rawData < rhs.rawData;
}

And the Header file:

#include "stdafx.h"
#include "cereal/archives/binary.hpp"
#include "cereal/types/memory.hpp"
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <stdio.h>


class RegDoneEntry
{
public:
    std::string rawData;
    int operation;
    template<class Archive>
    void serialize(Archive & archive)
    {
        // serialize things by passing them to the archive
        archive(rawData, operation);
    }
    friend bool operator< (const RegDoneEntry &left, const RegDoneEntry &right);

};


class RegDoneList
{
public:
    static std::set<RegDoneEntry> mylist;
    template<class Archive>
    void serialize(Archive & archive)
    {
        // serialize things by passing them to the archive
        archive(mylist);
    }
};
Jeff
  • 2,061
  • 4
  • 27
  • 45
  • What's error do you get? Also, whats use of ` template void serialize(Archive & archive)` member? – MKR Jan 06 '18 at 18:50
  • The error is "Cereal could not find any Output serialization functions". the template is what cereal requires. – Jeff Jan 06 '18 at 19:03
  • I had the same error. Turns out serialize was defined in my class as a private method, not a public. Followed a tutorial: https://rubentorresbonet.wordpress.com/2014/08/25/an-overview-of-data-serialization-techniques-in-c/ – alexpanter Jan 28 '21 at 12:07

1 Answers1

3

Class RegDoneEntry should have serialize method defined too.

struct RegDoneEntry
{
  std::string rawData;
  int operation;

  template<class Archive>
  void serialize (Archive& ) {  //... 
  } 
};

Next, you want to create set of RegDoneEntry objects, but your code will not be compiled without operator< for RegDoneEntry class. You should add also this operator in definition of RegDoneEntry

struct RegDoneEntry {
   // ...code from above
   bool operator < (const RegDoneEntry& ) const {
       // your comparison
   }
};

And the last thing, you have set as static member, but you didn't define it in source code, so you should add this line too

std::set<RegDoneEntry> RegDoneList::mylist;
rafix07
  • 20,001
  • 3
  • 20
  • 33
  • This is of great help but I have one error left. I will update the code above – Jeff Jan 06 '18 at 19:51
  • @Jeff `mylist` in `RegDoneList` is still as static member, you should add `std::set RegDoneList::mylist;` before `RegDoneList myRegDoneList;` in main.cpp file. Static variable of class must be defined in .cpp file of source code. – rafix07 Jan 06 '18 at 20:02