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?
The mutable_proposedsegments()
method returns a pointer, so you might be missing *
at the beginning - try with:
Journey::Journey(std::vector<gen_ProposedSegment *> proposedSegment) {
*this->mutable_proposedsegments() = {proposedSegment.begin(), proposedSegment.end()};
}
In addition, for this to work you'd need to have the input typed as std::vector<gen_ProposedSegment>
instead (and better use a const ref), i.e.:
Journey::Journey(const std::vector<gen_ProposedSegment>& proposedSegment) {
*this->mutable_proposedsegments() = {proposedSegment.begin(), proposedSegment.end()};
}
Alternatively, you'd need to insert the items in a for loop (see std::for_each
).