Karnivaurus
Karnivaurus

Reputation: 24121

Converting google::protobuf::RepeatedField<float> to float

I am using Google's Protobuf to create a C++ class called Foo, as follows:

message Foo
{
    optional int32 x = 1;
    repeated float y = 2;
}

In the created C++ class, this creates a member variable y of type const google::protobuf::RepeatedField<float>. Now, in my C++ code, I want to access the x and y variables of an instance of Foo, called foo. In this example, y is of length one, i.e. it contains one float:

int a = foo.x();
float b = foo.y();
float c = foo.y()[0];

Here, the first line works, but I get errors for both the second and third lines. I am just trying to get the value of this float that is stored in y.

How should I be doing this?

Thanks!

Upvotes: 2

Views: 4036

Answers (2)

Kenton Varda
Kenton Varda

Reputation: 45171

You want:

float b = foo.y(0);

(Stack overflow is complaining that my answer is to short...)

Upvotes: 1

psv
psv

Reputation: 697

You can iterate over your Y-s

for(auto value : Foo.y())
{
    //do something with value here
}

Upvotes: 1

Related Questions