在谷歌协议缓冲区中设置重复字段时出错

Error setting repeated fields in google protocol buffers

本文关键字:字段 出错 设置 谷歌 协议 缓冲区      更新时间:2023-10-16

我有一个消息类:

message Foo {
     repeated Foo2 field = 1;
}
//Foo2 is another message

在我的 cpp 文件中:

Foo* ci1;
Foo* ci2;
//call some function to to assign values to ci1->field(0)
function( ci1 )
//try to copy ci1-field(0) to ci2->field(0)
ci2->set_field( 0, ci1->field(0) );

但是我收到一条错误消息:错误:"Foo"没有名为"set_field"的成员为什么它以成员身份读取set_field而不是成员字段set_函数?我是协议缓冲区的新手,所以任何帮助将不胜感激!

谢谢!

试试*ci2->add_field() = ci1->field(0) .

更一般地说,您需要了解 protobuf 如何映射到类,以及如何处理每种字段。详细信息在文档中。