gpt4 book ai didi

Ada:案例子集共享共同属性的变体记录

转载 作者:行者123 更新时间:2023-12-02 17:47:37 25 4
gpt4 key购买 nike

我有以下代码,其中我只希望前两种情况共享一个公共(public)属性;但是,当我尝试使用此语法时,出现错误 "id"与第 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;

最佳答案

这个:

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;

关于Ada:案例子集共享共同属性的变体记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12762603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com