我正在使用 Google 的 Protobuf 创建一个名为 Foo
的 C++ 类,如下:
message Foo
{
optional int32 x = 1;
repeated float y = 2;
}
在创建的C++类中,这创建了一个成员变量y
类型 const google::protobuf::RepeatedField<float>
。现在,在我的 C++ 代码中,我想访问 x
和y
Foo
实例的变量,称为 foo
。在此示例中,y
长度为一,即它包含一个 float :
int a = foo.x();
float b = foo.y();
float c = foo.y()[0];
在这里,第一行有效,但第二行和第三行都出现错误。我只是想获取存储在 y
中的这个 float 的值.
我应该怎么做?
谢谢!
最佳答案
你想要:
float b = foo.y(0);
(堆栈溢出提示我的答案太短......)
关于c++ - 将 google::protobuf::RepeatedField<float> 转换为 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32874292/