我有以下代码,我只想在前两个案例中共享共同财产;但是,当我尝试使用这种语法时,我收到错误"id" conflicts with the declaration at line 11
:
type Shape (Which : Shape_Type := SQUARE) is
record
case Which is
when Square =>
id : Natural; -- Line 11
when Turnout =>
id : Natural; -- Line that causes error to be thrown
when Invalid =>
null;
end case;
end record;
答案 0 :(得分:5)
此:
type Shape (Which : Shape_Type := SQUARE) is
record
case Which is
when Square | Turnout =>
id : Natural;
when Invalid =>
null;
end case;
end record;
如果您以后希望Turnout
案例具有额外属性,则可以使用嵌套case
来执行此操作(但您仍需要涵盖所有备选方案):
type Shape (Which : Shape_Type := SQUARE) is
record
case Which is
when Square | Turnout =>
id : Natural;
case Which is
when Square =>
null;
when Turnout =>
Deg : Natural;
when Invalid =>
null;
end case;
when Invalid =>
null;
end case;
end record;