Reputation: 2071
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);
}
};
Upvotes: 2
Views: 3107
Reputation: 20936
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;
Upvotes: 3