首页 新闻 会员 周边

取protoubf字段时有的字段取不到

0
[待解决问题]

定义protobuf如下;
message Mypb
{
Exch exch = 1;
string id = 3;
double weight = 14;
uint32 index = 18;
}
enum Exch
{
BJ = 0;
TJ = 1;
NJ = 2;
}
代码为;

void Function((google::protobuf::Message&)msg)
{
const Descriptor* descriptor = msg.GetDescriptor();
const Reflection* reflection = msg.GetReflection();
const uint count = descriptor->field_count();
std::cout<<"count= "<<count<<std::endl;
for (int i = 1; i <= count; ++i)
{
const FieldDescriptor* goal_field = descriptor->FindFieldByNumber(i);
if(nullptr==goal_field)
{
continue;
}
std::cout<<"goal_field->name(): "<<goal_field->name();
}
}
int main()
{
Mypb tmp_comp;
tmp_comp.set_id("12345");
tmp_comp.set_weight(122.43);
tmp_comp.set_index(231321);
tmp_comp.set_exch(static_cast< Exch >(2));
Function((google::protobuf::Message&)tmp_comp);
}

 

此时count=4,那么for循环中调用FindFieldByNumber()时所能取到的字段号最大为4,那么就无法取到字段号为14的weight和18的high,这种不连续字段号的message该如何取值的?谢谢

CLFAL的主页 CLFAL | 初学一级 | 园豆:178
提问于:2017-04-25 16:21
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册