user2138149
user2138149

Reputation: 17170

Google Protocol Buffers: No operator[]?

According to the google developer docs the protocol buffer should have an operator[].

I have only just started working with them, and so perhaps I have not fully understood something. The application is for "caffe" (machine learning program) data files. (I am trying to use them to write datafiles which caffe can then read.)

I have this line of code in my program (1)

datumFloatData->operator[](caffe_ix) = (float)(0.5 * (z_pos + 1.0));

defined by

google::protobuf::RepeatedField<float>* datumFloatData = \
    datum.mutable_float_data();

this compiles ok:

for(int32_t ix{0}; ix < ix_max; ++ ix)
{
    datumFloatData->Add(-1.0f);
}

however (1) does not compile, the error is:

error: ‘class google::protobuf::RepeatedField<float>’ has no member
named ‘operator[]’; did you mean ‘operator=’?
    datumFloatData->operator[](caffe_ix) = (float)(0.5 * (z_pos + 1.0));

What's going on here? I can use the Add() method without issue, but operator[] is not defined?

Edit: This also does not work:

google::protobuf::RepeatedField<float>& \ 
    datumFloatData_ref{*datumFloatData};
datumFloatData_ref[2] = 1.0f;

Edit 2: A fix that works

*datumFloatData->Mutable(caffe_ix) = (float)(0.5 * (z_pos + 1.0));

Upvotes: 2

Views: 988

Answers (1)

MSalters
MSalters

Reputation: 179981

You probably have an older version of protobuf. A quick check shows that operator[] is a trivial wrapper around Get, and fairly new. It was added 2016-09-16

Upvotes: 2

Related Questions