我必须通过boost::shared_ptr
:如何将protobuf的boost :: shared_ptr指针传递给函数?
boost::shared_ptr<Protobuf::Person::Profile> pProfile =
boost::make_shared<Protobuf::Person::Profile>();
是的protobuf的指针,在protobuf的函数oPerson.set_allocated_profile(pProfile)
但oPerson.set_allocated()
需要一个指向Protobuf::Person::Profile
。
我尝试了几种方法,但我认为当我尝试使用pbjson::pb2Json
这是一个基于快速json构建的库函数将protobuf对象转换为JSON时,指针超出范围而导致分段错误。
方法1:
oPerson.set_allocated_profile(pProfile.get());
方法2:
oPerson.set_allocated_profile(&*pProfile);
努力帮助没有更多的背景:回顾你的一生的范围并确保内存在使用时仍然有效。 –