I have the following protobuf message :
message gen_Journey {
repeated gen_ProposedSegment proposedSegments = 1;
}
the generated cpp is the following
// repeated .gen_ProposedSegment proposedSegments = 1;
int gen_Journey::proposedsegments_size() const {
return proposedsegments_.size();
}
void gen_Journey::clear_proposedsegments() {
proposedsegments_.Clear();
}
const ::gen_ProposedSegment& gen_Journey::proposedsegments(int index) const {
// @@protoc_insertion_point(field_get:gen_Journey.proposedSegments)
return proposedsegments_.Get(index);
}
::gen_ProposedSegment* gen_Journey::mutable_proposedsegments(int index) {
// @@protoc_insertion_point(field_mutable:gen_Journey.proposedSegments)
return proposedsegments_.Mutable(index);
}
::gen_ProposedSegment* gen_Journey::add_proposedsegments() {
// @@protoc_insertion_point(field_add:gen_Journey.proposedSegments)
return proposedsegments_.Add();
}
::google::protobuf::RepeatedPtrField< ::gen_ProposedSegment >*
gen_Journey::mutable_proposedsegments() {
// @@protoc_insertion_point(field_mutable_list:gen_Journey.proposedSegments)
return &proposedsegments_;
}
const ::google::protobuf::RepeatedPtrField< ::gen_ProposedSegment >&
gen_Journey::proposedsegments() const {
// @@protoc_insertion_point(field_list:gen_Journey.proposedSegments)
return proposedsegments_;
}
I created the following vector :
std::vector<gen_ProposedSegment *> proposedSegment
based on Copy a std::vector to a repeated field from protobuf with memcpy I did the following :
Journey::Journey(std::vector<gen_ProposedSegment *> proposedSegment) {
this->mutable_proposedsegments() = {proposedSegment.begin(), proposedSegment.end()};
}
the problem is that i am getting the following error:
/home/compilation/UnixPackagesFareShopping/src/DOM/src/journey.cpp:10:35: error: lvalue required as left operand of assignment
What am I doing wrong?
You have to iterate of the given vector and add the objects to your protobuf message manually. You can not use a memcpy operation for that.
Following code is written out of my mind without testing ... but should point you in the correct direction. btw: I assume Journey
inherits from gen_Journey
in this case. Otherwise you have to adjust the "this->" statement accordingly.
Journey::Journey(const std::vector<gen_ProposedSegment *> &proposedSegment) {
auto copy = [&](const gen_ProposedSegment *) {
auto temp_seg = this->add_proposedsegments();
temp_seg->CopyFrom(*gen_ProposedSegment);
};
std::for_each(proposedSegment.cbegin(), proposedSegment.cend(), copy);
}