Protocol buffers 向重复消息添加必填字段时出现问题

Protocol buffers 向重复消息添加必填字段时出现问题,protocol-buffers,Protocol Buffers,我使用的是C++11。我的问题是,我无法将必填字段添加到重复消息中 Sample.Proto message HDPMsg {    required MsgHeaderSig messageHeader = 1;    optional int64 tag = 18999; } message InfusionEventContainer {    repeated InfusionEventSig infusionOngoingEvents = 5;    optional int64 ta

我使用的是C++11。我的问题是,我无法将必填字段添加到重复消息中

Sample.Proto
message HDPMsg
{
   required MsgHeaderSig messageHeader = 1;
   optional int64 tag = 18999;
}
message InfusionEventContainer
{
   repeated InfusionEventSig infusionOngoingEvents = 5;
   optional int64 tag = 18999;
}

message InfusionEventSig
{
   required MsgHeaderSig messageHeader = 1;
   optional int64 tag = 18999;
}

Sample.cpp
void buildMessage(HdpMain &rHdpMain)
{
   HDPMsg hdp5;
   MsgHeaderSig header;
   InfusionEventContainer container;
   InfusionEventSig infusionOngoingEvents;


   header.set_timestamp(time(NULL));
   header.set_sourceid(MSG_SOURCE_UIC);


   hdp5.set_allocated_messageheader(&header);
   hdp5.set_eventtype(INFUSION_UPDATE);

   container.set_allocated_messageheader(&header); // here i am getting error 
   when running code.Error: header not set.

   infusionOngoingEvents.set_allocated_messageheader(&header);

   container.add_infusionongoingevents();

   hdp5.set_allocated_infusionevents(&container);

    printf("is infusionongoingevents IsInitialized after= %d\n", 
  hdp5.infusionevents().infusionongoingevents(0).IsInitialized()); // giveng me value 0(not set).
    // if i use printf("is infusionongoingevents IsInitialized after= %d\n",infusionOngoingEvents.IsInitialized()); // giving me value 1(set).

   hdp5.release_messageheader();
   hdp5.release_infusionevents();

   container.release_messageheader();
   container.clear_infusionongoingevents();
   container.release_shifttotals();

   infusionOngoingEvents.release_messageheader();
 
}
错误:[libprotobuf FATAL google/protobuf/message_lite.cc:231]检查失败:IsInitialized():无法序列化类型为“com.silver.hdp5signals.mdcodegen.signals.HDPMsg”的消息,因为它缺少必填字段:infusionEvents.infusionOngoingEvents[0]。messageHeader

我得到了答案。 对于重复字段,我们必须获取空指针并向该指针添加值

例如:InfusionEventSig*infusionOngoingEvents=container.add_infusionOngoingEvents()


这解决了问题

对不起,我在infusionOngoingEvents时出错。设置\u分配的\u messageheader(&header);