How to get this generic package to compile

Question

I have the program to where it is reading the generic package, but I cannot figure out what to do about the context clauses to get the numbers to print and/or be read in from the file. In the client program right now, I have that the Element_Type should be an integer, but all my declarations are of type element, how do I do anything with this when there is no Ada.Element_text_IO; please let me know, I've been stuck for quite a while.

client program

WITH Ada.Text_IO; 
WITH Ada.Integer_Text_IO; 
WITH Ada.Float_Text_IO; 
WITH Min_Max;

PROCEDURE Min_Max_Average_File IS
------------------------------------------------------------------
--| Finds and displays the minimum, maximum, and average
--| of a list of data items; the data items are read from a file.                                   
------------------------------------------------------------------

package Grades is new Min_Max(Element_Type => Integer);
use Grades;



  NumValues:    Element;  -- input - the number of items to be averaged
  CurrentValue: Element;   -- input - the next data item to be added   
  Smallest:     Element;   -- output - minimum of the data values
  Largest:      Element;   -- output - maximum of the data values
  Average:      Element;   -- output - average of the data values
  Sum:          Element;   -- program variable - the sum being accumulated   
  TestScores:   Ada.Text_IO.File_Type; 

BEGIN  -- Min_Max_Average_File
  -- Open the file and associate it with the file variable name
  Ada.Text_IO.Open
    (File => TestScores, Mode => Ada.Text_IO.In_File, Name => "scores.txt");

  -- Read from the file the number of items to be averaged
           Ada.Integer_Text_IO.Get(File => TestScores, Item => NumValues);  
           Ada.Text_IO.Put("The number of scores to be averaged is ");
           Ada.Integer_Text_IO.Put(Item => NumValues, Width => 1);  
           Ada.Text_IO.New_Line;
  -- Initialize program variables
  Smallest := Element'Last;
  Largest := Element'First;
  Sum := 0;

  -- Read each data item, log to the screen, add it to Sum, 
  -- and check if it is a new minimum or maximum
  FOR Count IN 1 .. NumValues LOOP
    Ada.Integer_Text_IO.Get(File => TestScores, Item => CurrentValue);
    Ada.Text_IO.Put("Score number ");
    Ada.Integer_Text_IO.Put(Item => Count, Width => 1);  
    Ada.Text_IO.Put(" is ");
    Ada.Text_IO.Put(Item => CurrentValue, Width => 1);  
    Ada.Text_IO.New_Line;
    Sum := Sum + CurrentValue;
    Smallest := Grades.Minimum(Value1 => Smallest, Value2 => CurrentValue);
    Largest  := Grades.Maximum(Value1 => Largest,  Value2 => CurrentValue);

  END LOOP;

  -- compute the average; since Sum and NumValues are integers,
  -- the average is truncated, that is, the fractional part is discarded
  Average := Sum / NumValues;

  -- display the results
  Ada.Text_IO.Put(Item => "The Smallest is ");
  Ada.Integer_Text_IO.Put(Item => Smallest, Width => 1);  
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put(Item => "The Largest is ");
  Ada.Integer_Text_IO.Put(Item => Largest, Width => 1);  
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put(Item => "The Average is ");
  Ada.Integer_Text_IO.Put(Item => Average, Width => 1);  
  Ada.Text_IO.New_Line;

END Min_Max_Average_File;

package spec

generic

  Type Element_Type is private;


PACKAGE Min_Max IS

------------------------------------------------------------------
--| specifications of functions provided by Min_Max package                                     
------------------------------------------------------------------
  Type Element is new Element_Type;
  FUNCTION Minimum (Value1, Value2: Element_Type) RETURN Element_Type;
  -- Pre:  Value1 and Value2 have been assigned values
  -- Post: Returns the smaller of the two input values

  FUNCTION Maximum (Value1, Value2: Element_Type) RETURN Element_Type;
  -- Pre:  Value1 and Value2 have been assigned values
  -- Post: Returns the larger of the two input values

package body

PACKAGE BODY Min_Max IS
------------------------------------------------------------------
--| bodies of functions provided by Min_Max package                                     
------------------------------------------------------------------

  FUNCTION Minimum (Value1, Value2: Element_Type) RETURN Element_Type IS
    Result: Element_Type;
  BEGIN

    IF Value1 < Value2 THEN
       Result := Value1;
    ELSE
       Result := Value2;
    END IF;
    RETURN Result;

  END Minimum;

  FUNCTION Maximum (Value1, Value2: Element_Type) RETURN Element_Type IS
    Result: Element_Type;
  BEGIN

    IF Value1 > Value2 THEN
       Result := Value1;
    ELSE
       Result := Value2;
    END IF;
    RETURN Result;

  END Maximum;

END Min_Max;

Show source
| generics   | ada   | package   2016-11-14 18:11 2 Answers

Answers ( 2 )

  1. 2016-11-14 19:11

    When I try to compile the specification for Min_Max, I get the error message:

    min_max.ads:16:71: missing "end Min_Max;"
    

    Fixing that should be quite trivial.

  2. 2016-11-14 20:11

    When you say

    package Grades is new Min_Max(Element_Type => Integer);
    

    this means that the functionality of Grades is that of Min_Max, for Integers.

    So you shouldn’t declare NumValues etc. of type Element, but instead of type Integer. This will resolve your problems with being able to read/write values.

    However, you will have problems compiling the generic. You’ve declared Element_Type as private; this restricts what you can do with objects of this type to assignment and comparison for equality. It does not support "<", "<=", ">=", ">".

    If you’re happy to support signed integer types, you could say (ARM 12.5.2(3))

    type Element_Type is range <>;
    

    Alternatively (say you wanted to be able to support floating point types as well) you would need to import the relevant operations (ARM 12.6(18)):

    type Element_Type is private;
    with function "<" (L, R : Element_Type) return Boolean is <>;
    with function ">" (L, R : Element_Type) return Boolean is <>;
    

    (the is <> means to use the operation of Element_Type with the same name, if there is one).

◀ Go back