C++ 如何将向量添加到重复场协议c++;

C++ 如何将向量添加到重复场协议c++;,c++,protobuf-c,C++,Protobuf C,我有以下protobuf消息: message gen_Journey { repeated gen_ProposedSegment proposedSegments = 1; } 生成的cpp如下所示 // repeated .gen_ProposedSegment proposedSegments = 1; int gen_Journey::proposedsegments_size() const { return proposedsegments_.size(); } void

我有以下protobuf消息:

message gen_Journey {
  repeated gen_ProposedSegment proposedSegments = 1;
}
生成的cpp如下所示

// 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_;
}


我做错了什么

方法
mutable\u proposedsegments()
返回一个指针,因此您可能在开始时缺少
*
-请尝试:

Journey::Journey(std::vector<gen_ProposedSegment *> proposedSegment) {
    *this->mutable_proposedsegments() = {proposedSegment.begin(), proposedSegment.end()};
}
旅程::旅程(标准::建议的向量段){
*this->mutable_proposedsegments()={proposedSegment.begin(),proposedSegment.end()};
}
此外,要使其工作,您需要将输入键入为
std::vector
(最好使用const ref),即:

旅程::旅程(const std::vector和proposedSegment){
*this->mutable_proposedsegments()={proposedSegment.begin(),proposedSegment.end()};
}

或者,您需要在for循环中插入项(请参见
std::for_each
)。

您必须迭代给定向量,并手动将对象添加到protobuf消息中。您不能对此使用memcpy操作

下面的代码是我写出来的,没有经过测试。。。但是应该给你指出正确的方向。顺便说一句:在这种情况下,我假设
旅程
继承自
gen\u旅程
。否则,您必须相应地调整“this->”语句

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);
}
旅程::旅程(const std::vector和proposedSegment){
自动复制=[&](const gen_ProposedSegment*){
自动临时分段=此->添加建议分段();
临时分段->复制自(*建议分段);
};
std::for_each(proposedSegment.cbegin(),proposedSegment.cend(),副本);
}

如果我这样做,我会得到以下错误:错误:与“运算符=”不匹配(操作数类型为“gen_ProposedSegment”和“gen_ProposedSegment*”),因此,您还需要将输入向量键入为
std::vector
(如果可能)或手动迭代。迭代时,我应该使用可变的_proposedsegments(int index)?对于迭代版本,我认为您需要使用
add\u proposedsegments()
。如果索引>=实际大小,我不确定
mutable\u proposedsegments(int index)
是否会实际添加该项。非常感谢!你真的很有帮助。多谢各位
/home/compilation/UnixPackagesFareShopping/src/DOM/src/journey.cpp:10:35: error: lvalue required as left operand of assignment
Journey::Journey(std::vector<gen_ProposedSegment *> proposedSegment) {
    *this->mutable_proposedsegments() = {proposedSegment.begin(), proposedSegment.end()};
}
Journey::Journey(const std::vector<gen_ProposedSegment>& proposedSegment) {
    *this->mutable_proposedsegments() = {proposedSegment.begin(), proposedSegment.end()};
}
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);
}