adaada95

Expected private type


I have a compile-error that states: "expected private type "Pekare" defined at line 3. found an access type".

this is the .ads code:

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;

Solution

  • The problem is that the Data_Access type is private and not known to be an access type in the visible ("public") part of the spec. Therefore, you cannot assign null to the Data field as this would presume the Data field to be of an access type. The typical solution is to use a deferred constant. Such a constant can be declared and used in the public part of the spec, but it's full definition (i.e. it's actual value) is deferred to the private part. Below an example:

    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;