gpt4 book ai didi

task - Ada 左手赋值不能是受限类型

转载 作者:行者123 更新时间:2023-12-02 21:16:49 25 4
gpt4 key购买 nike

花了几个小时在这上面后我感到不知所措。

protected 类型声明:

   protected type AdditionMachine is
procedure setTask (Item : in WorkerTask);
procedure getTask (Item : out WorkerTask);
procedure calculate;
private
machineType : String (1 .. 1) := "A";
job : workerTask;
end AdditionMachine;

任务:

   task body SimpleTask1 is 
Available : Natural := ADDITION_MACHINES;
Elements : array (1 .. ADDITION_MACHINES) of AdditionMachine;
begin
loop
select
when Available > 0 =>
accept getMachine (machine : out AdditionMachine) do
machine := Elements(Available);
Available := Available - 1;
end getMachine;
or
accept addMachine (machine : in AdditionMachine) do
Available := Available + 1;
Elements(Available) := machine;
end addMachine;
end select;
end loop;
end SimpleTask1;

Elements(Available) := machine;machine := Elements(Available); 行,我得到“左手分配不得限制类型”

我不知道如何解决这个问题,我在谷歌搜索时什么也没找到,有人可以帮忙吗?

编辑:我现在知道 protected 类型是有限的,但是如何在没有上述内容的情况下完成 protected 对象池?

最佳答案

来自 ARM 7.5 ,有限类型,

A limited type is (a view of) a type for which copying (such as for an assignment_statement) is not allowed. A nonlimited type is a (view of a) type for which copying is allowed.

但是,您可以复制对有限对象的访问权限。

所以,如果您已声明

protected type AdditionMachine is
...
end AdditionMachine;
type AdditionMachine_Access is access AdditionMachine;

你可以写这样的东西(我没有测试过):

task body SimpleTask1 is 
Available : Natural := ADDITION_MACHINES;
Elements : array (1 .. ADDITION_MACHINES) of AdditionMachine_Access
:= (others => new AdditionMachine);
begin

当你使用完分配的对象后,你需要负责释放它们(如果程序需要无限期地运行的话)。我认为 protected 对象不会有问题(但是如果任务正在等待您解除分配的 PO 会发生什么?),并且最近的 GNAT 可以处理解除分配任务,前提是任务具有开放的终止替代方案 ARM 9.7.1 ,或者已经终止 - 也许是被中止)。

关于task - Ada 左手赋值不能是受限类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30155313/

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