gpt4 book ai didi

c++ - Cereal 找不到任何输出序列化函数

转载 作者:行者123 更新时间:2023-11-28 04:51:28 26 4
gpt4 key购买 nike

我不知道为什么在尝试编译时会出现此错误。我尝试添加几乎所有类型。我正在尝试序列化一组 RegDoneEntry 的 .我知道序列化的调用不在这里,但我不能超越这个。以下内容已更新以反射(reflect)建议。

我现在得到的错误是

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;
}

和头文件:

#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);
}
};

最佳答案

RegDoneEntry应该有 serialize方法也定义了。

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

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

接下来,您要创建一组 RegDoneEntry 对象,但如果没有 operator<,您的代码将不会被编译。对于 RegDoneEntry 类。您还应该在 RegDoneEntry 的定义中添加此运算符

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

最后一件事,你已经设置为静态成员,但你没有在源代码中定义它,所以你也应该添加这一行

std::set<RegDoneEntry> RegDoneList::mylist;

关于c++ - Cereal 找不到任何输出序列化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com