本文介绍了如何在capnproto C ++生成的代码中设置列表的字符串项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的capnproto定义:
I have capnproto definition like this:
struct School {
name @0 :Text;
address @1 :Address;
foundation @2 :Date;
emailAddresses @3 :List(Text);
}
我想在构建器中使用与此类似的代码设置emailAddresses字段(但这不会编译):
I would like to set the emailAddresses field in a builder with code similar to this (but this won't compile):
static School::Builder random_School() {
capnp::MallocMessageBuilder msg;
School::Builder result = msg.initRoot<School>();
result.setName(rand_str(36));
result.setAddress(random_Address());
result.setFoundation(random_Date());
result.initEmailAddresses(item_count);
for (size_t i = 0; i < item_count; ++i) {
result.getEmailAddresses()[i] = rand_str(37); // rand_str returns std::string
}
return result;
}
正确的方法是什么?
推荐答案
根据capnproto 文档在列表"部分中,您应该使用builder.set(index,value).
According to the capnproto documentation in the Lists section, you should use builder.set(index, value).
result.getEmailAddresses().set(i, rand_str(37));
我想它应该现在编译.
这篇关于如何在capnproto C ++生成的代码中设置列表的字符串项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!