C++11 Can';将std::vector序列化为谷物
我对序列化还不熟悉,而且我在用。下面的示例说明了问题:C++11 Can';将std::vector序列化为谷物,c++11,serialization,cereal,C++11,Serialization,Cereal,我对序列化还不熟悉,而且我在用。下面的示例说明了问题: class MyClass { int x, y, z; class MyOtherClass { string name, description; public: template<class Archive> void serialize(Archive & archive) { archi
class MyClass
{
int x, y, z;
class MyOtherClass
{
string name, description;
public:
template<class Archive>
void serialize(Archive & archive)
{
archive(name, description);
}
};
vector<MyOtherClass> Victor;
vector<int> ints;
public:
template<class Archive>
void serialize(Archive & archive)
{
archive(x, y, z, ints); // error C2338: cereal could not find any output serialization functions for the provided type and archive combination.
}
};
我做错了什么?很可能是因为您没有阅读文档部分,忘记了包含相关的头文件,例如
#include
。你的例子不完整,但这是我的猜测。斯佩克拉斯,你说得对,谢谢。我只阅读了“快速入门”页面和文档主页上链接的其他几页。我现在没有时间测试代码现在是否工作,但我预计不会有任何进一步的问题。再次感谢!
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#include <cereal/archives/json.hpp>
#include <cereal/types/vector.hpp>
// See details in http://uscilab.github.io/cereal/stl_support.html
class MyClass {
int x, y, z;
class MyOtherClass {
string name, description;
public:
template <class Archive>
void serialize( Archive &archive )
{
archive( CEREAL_NVP( name ), CEREAL_NVP( description ) );
}
};
vector<MyOtherClass> Vector;
vector<int> ints;
public:
template <class Archive>
void serialize( Archive &archive )
{
archive( CEREAL_NVP( x ), CEREAL_NVP( y ), CEREAL_NVP( z ), CEREAL_NVP( ints ) );
}
// Add one element to the private vector
void populateVector( const int value ) {
ints.push_back( value );
}
};
int main()
{
MyClass MyObject{};
MyObject.populateVector( 101 );
MyObject.populateVector( 202 );
MyObject.populateVector( 303 );
// For brevity I just print the serialization to the standard output instead of the binary file
cereal::JSONOutputArchive oarchive( cout );
oarchive( MyObject );
return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#include <cereal/archives/json.hpp>
#include <cereal/types/vector.hpp>
// See details in http://uscilab.github.io/cereal/stl_support.html
class MyClass {
int x, y, z;
class MyOtherClass {
string name, description;
public:
template <class Archive>
void serialize( Archive &archive )
{
archive( CEREAL_NVP( name ), CEREAL_NVP( description ) );
}
};
vector<MyOtherClass> Vector;
vector<int> ints;
public:
template <class Archive>
void serialize( Archive &archive )
{
archive( CEREAL_NVP( x ), CEREAL_NVP( y ), CEREAL_NVP( z ), CEREAL_NVP( ints ) );
}
// Add one element to the private vector
void populateVector( const int value ) {
ints.push_back( value );
}
};
int main()
{
MyClass MyObject{};
MyObject.populateVector( 101 );
MyObject.populateVector( 202 );
MyObject.populateVector( 303 );
// For brevity I just print the serialization to the standard output instead of the binary file
cereal::JSONOutputArchive oarchive( cout );
oarchive( MyObject );
return 0;
}
{
"value0": {
"x": 0,
"y": 0,
"z": 0,
"ints": [
101,
202,
303
]
}
}