协议缓冲区:枚举问题 [英] Protocol Buffer: Enum issue
问题描述
我有以下 .proto 文件:
I have the following .proto file :
enum Enum1{
X=0;
Y=1;
}
message SomeClass{
required Enum1 enum1=1;
required Enum2 enum2=2;
}
enum Enum2{
X=0;
Z=1;
}
当我尝试使用 protoc 编译它时,出现以下错误:
When I try to comile it using protoc , I get the following error :
proto.proto:19:5: "X" 已经定义 proto.proto:19:5: 注意枚举值使用 C++ 范围规则,这意味着枚举值是他们类型的兄弟姐妹,而不是它的孩子.因此,X"必须是unique ,而不仅仅是在Enum2"中.
proto.proto:19:5: "X" is already defined proto.proto:19:5: Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it. Therefore, "X" must be unique , not just within "Enum2".
我有什么办法可以克服这个问题!
I there any way I could overcome this issue !
推荐答案
您可以将您的枚举包含在另一条消息中,这样可见性就不会发生冲突.
You could include your enum inside another message so the visibility will not conflict.
示例:
message Enum1{
enum Enum{
X=0;
Y=1;
}
}
message Enum2{
enum Enum{
X=0;
Y=1;
}
}
message SomeClass{
required Enum1.Enum enum1=1;
required Enum2.Enum enum2=0;
}
你也可以用一些东西作为你的枚举值的前缀.如果您不更改值名称后的数字,它应该与您的旧版本保持兼容:例如:
You could also prefix your enum value with something. If you don't change the number after the name of your value, it should stay compatible with your old version : ex:
enum Enum1{
E1_X=0;
E1_Y=1;
}
enum Enum2{
E2_X=0;
E2_Z=1;
}
message SomeClass{
required Enum1 enum1=1;
required Enum2 enum2=2;
}
这篇关于协议缓冲区:枚举问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!