我不知道为什么我在尝试编译时遇到此错误。我尝试添加几乎所有类型。我正在尝试序列化一组RegDoneEntry。我知道序列化的调用不在这里,但我不能超越这个。以下内容已更新,以反映建议。
我现在得到的错误是
严重级代码描述项目文件行抑制状态 错误LNK2001未解析的外部符号 “public:static class std :: set,class std :: allocator&gt; RegDoneList :: mylist” (?mylist @RegDoneList @@ 2V?$ set @ VRegDoneEntry @@ U?$ less @ VRegDoneEntry @@@ std @@ V?$ allocator @ VRegDoneEntry @@@ 3 @@ std @@ A)CerealTest CerealTest.obj 1 < / p>
#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;
}
Header文件:
#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);
}
};
答案 0 :(得分:1)
班级RegDoneEntry
也应定义serialize
方法。
struct RegDoneEntry
{
std::string rawData;
int operation;
template<class Archive>
void serialize (Archive& ) { //...
}
};
接下来,您要创建一组RegDoneEntry对象,但是对于RegDoneEntry类,如果没有operator<
,则不会编译代码。
您应该在RegDoneEntry
struct RegDoneEntry {
// ...code from above
bool operator < (const RegDoneEntry& ) const {
// your comparison
}
};
最后一件事,你已经设置为静态成员,但是你没有在源代码中定义它,所以你也应该添加这一行
std::set<RegDoneEntry> RegDoneList::mylist;