C ++ msgpack用户定义的类-无法入门 [英] C++ msgpack User-defined classes - can't get started

查看:216
本文介绍了C ++ msgpack用户定义的类-无法入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找msgpack的C ++快速入门指南.

I've been looking at the C++ quick-start guide for msgpack.

http://wiki.msgpack.org/pages/viewpage.action? pageId = 1081387

这里有以下代码段:

#include <msgpack.hpp>
#include <vector>
#include <string>

class myclass {
private:
    std::string str1;
    std::string str2;
public:
    MSGPACK_DEFINE(str1,str2);
};

int main(void) {
        std::vector<myclass> vec;
        // add some elements into vec...
        /////
        /* But what goes here??? */
        /////

        // you can serialize myclass directly
        msgpack::sbuffer sbuf;
        msgpack::pack(sbuf, vec);

        msgpack::unpacked msg;
        msgpack::unpack(&msg, sbuf.data(), sbuf.size());

        msgpack::object obj = msg.get();

        // you can convert object to myclass directly
        std::vector<myclass> rvec;
        obj.convert(&rvec);
}

我要序列化myclass对象的向量.

I want to serialize a vector of myclass objects.

我尝试了以下操作:

...
vector<myclass> rb;
myclass mc;

...

int main(){
    ...
    mc("hello","world");
    rb.push_back(mc)
    ...
}

但这不起作用(错误:通话不匹配")

But this doesn't work ("error: no match for call")

此外,如果我这样做:

mc.str1="hello"
mc.str2="world"

因为str1和str2是私有的,所以它不起作用.

it won't work as str1 and str2 are private.

如何正确使用此MSGPACK_DEFINE(...)宏?我似乎在网上找不到任何东西.

How to use this MSGPACK_DEFINE(...) macro properly? I can't seem to find anything online.

非常感谢,

推荐答案

MSGPACK_DEFINE()定义了一些为您的类实现打包和解包的方法.您在()中放置的是要序列化的成员的列表.

MSGPACK_DEFINE() defines some methods implementing packing and unpacking for your class. What you put inside () is a list of the members you want serialized.

此后,您可以打包和解压缩类,就像打包或解压缩int一样.因此,该示例应该可以正常工作.

After that, you can pack and unpack your class just like you would pack or unpack an int. So the example should be working.

您可以尝试删除矢量并仅打包一个对象-我认为这样可以简化它.

You can try removing the vector and pack only a single object - I think that would simplify it.

这篇关于C ++ msgpack用户定义的类-无法入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆