期望私有类型

Expected private type

我有一个编译错误,指出:“在第 3 行定义的预期私有类型“Pekare”。找到了一个访问类型”。

这是 .ads 代码:

package Image_Handling is

   type Data_Access is private;
   
   type Image_Type is record
      X_Dim, Y_Dim : Integer;
      Data : Data_Access := null;
   end record;
   type RGB_Type is private;

functions...

private

   type RGB_Type is record
      R, G ,B : Natural;
      T : Boolean := False;
      end record;
   
   type Undim_Array is array(Positive range <>, Positive range <>) of RGB_Type;
   
   type Data_Access is access Undim_Array;
   
end Image_Handling;

问题是 Data_Access 类型是私有的,在规范的可见部分 ("public") 中不知道它是访问类型。因此,您不能将 null 分配给 Data 字段,因为这会假定 Data 字段属于访问类型。典型的解决方案是使用 延迟常量 。这样的常量可以在规范的 public 部分声明和使用,但它的完整定义(即它的实际值)被推迟到私有部分。下面是一个例子:

image_handling.ads

package Image_Handling is

   type Data_Access is private;
   None : constant Data_Access;   --  Deferred constant.
   
   type Image_Type is record
      X_Dim, Y_Dim : Integer;
      Data         : Data_Access := None;
   end record;
   type RGB_Type is private;   
   
private

   type RGB_Type is record
      R, G ,B : Natural;
      T       : Boolean := False;
   end record;
   
   type Undim_Array is array(Positive range <>, Positive range <>) of RGB_Type;
   
   type Data_Access is access Undim_Array;
   None : constant Data_Access := null; 
   -- ^^^ At this point we know that Data_Access is an access type and
   --     provide a full definition of the constant that states it's value.
   
end Image_Handling;