You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何重命名Ada中受保护对象的Synchronized_Queue_Interface Enqueue入口

错误原因

你写的代码不合法是因为Ada语言规则不允许在包声明区域直接将保护对象实例的操作重命名为包级子程序。Ada.Containers提供的同步队列属于保护类型,你声明的Queue是该类型的具体实例,它的Enqueue是保护操作,无法直接通过renames绑定为包级过程。

解决方案

通过简单的包装过程就能实现你的需求,修改后的完整代码如下:

包规范文件(test.ads)

with Ada.Containers.Synchronized_Queue_Interfaces;
with Ada.Containers.Unbounded_Synchronized_Queues;

package Test is
   
   use Ada.Containers;
   
   package Boolean_Queue_Interfaces is new Synchronized_Queue_Interfaces
     (Element_Type => Boolean);
   
   package Boolean_Queues is new Unbounded_Synchronized_Queues
     (Queue_Interfaces => Boolean_Queue_Interfaces);
   
   Queue : Boolean_Queues.Queue;
   
   procedure Enqueue (New_Item : Boolean);

end Test;

包体文件(test.adb)

package body Test is

   procedure Enqueue (New_Item : Boolean) is
   begin
      Queue.Enqueue(New_Item);
   end Enqueue;

end Test;
补充说明

如果不需要对外暴露原始Queue实例,可以把Queue移到包体内部,只暴露自定义的操作接口,实现更高程度的封装。

内容的提问来源于stack exchange,提问作者user15552120

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 08:24:02