Roy Diamond
Roy Diamond

Reputation: 11

No candidate interpretations match the actuals, problem coding with generics in ADA

Having some trouble with code not compiling due to print statements with put. Will post code below.

--  createlis.ads

generic
   size: integer;
   type itemtype is private;
          
package createlis is
           
   procedure addtolist( ad : in itemtype );
       
   procedure printlist;
      
   procedure printlist1(pt: in integer );
       
   procedure deletelist(pt: in integer );
       
   procedure listlength;
          
end createlis;
--  createlis.adb
 
with ada.text_io; use ada.text_io;


package body createlis is
       
       len: integer;
       list: array(1..size) of itemtype;
       
       procedure addtolist( ad : in itemtype ) is 
       begin
          if len < size then
             len := len +1;
             list(len) := ad;
             end if;
       end addtolist;
       
       procedure printlist is 
       begin
          for i in 1..len loop
           put((list(i)));
             put(" , ");
             end loop;
       end printlist;
       
       
       procedure printlist1( pt: in integer ) is 
       begin
          put((list(pt)));
       end printlist1;
       
       
      procedure deletelist( pt: in integer) is
       begin
          for k in pt..(len-1) loop
             list(k) := list(k+1);
                end loop;
                len := len - 1;
       end deletelist;
       
       procedure listlength is
          i : integer;
       begin
          i := 0;
          for x in 1..size loop
             i := i + 1;
          end loop;
          put("the array is ");
          put(integer'image(i));
          put(" numbers.");
          
       end listlength;
       
    end createlis;
--  main.adb

with Ada.Text_IO; use Ada.Text_IO;
with createlis; 
procedure main is
    
   package iio is new ada.text_io.integer_io(integer); use iio;
   package iii is new ada.text_io.float_io(float); use iii;
    
   size: integer;
   
begin
    
   put("enter size for option c: ");
   get(size);
    
   declare
              
      package clist is new createlis(size, integer); use clist;
    
   begin
      clist.printlist;
      clist.addtolist(12);
      clist.addtolist(3);
      clist.addtolist(7);
      clist.printlist;
      new_line;
      clist.printlist1(2);
      new_line;
      clist.listlength;
      new_line;
      clist.deletelist(2);
      clist.printlist;
    
   end;
    
   declare
          
      package blist is new createlis(size, float); use blist;
    
   begin
    
      blist.printlist;
      blist.addtolist(12.7);
      blist.addtolist(3.5);
      blist.addtolist(7.6);
      blist.printlist;
      new_line;
      blist.listlength;
      new_line;
      blist.deletelist(2);
      blist.printlist;
   end;

end main;

Upvotes: 0

Views: 928

Answers (1)

flyx
flyx

Reputation: 39768

Ada.Text_IO does not contain a Put function for Integer nor for Float. While it contains the generic packages Integer_IO and Float_IO, you can't directly use them because your generic package can take either kind of type.

This means that you must tell your generic package on instantiation how to put the itemtype, e.g.:

generic
   size: integer;
   type itemtype is private;
   with procedure Put (Value : itemtype);
package createlis is
-- ...

Then inside your main, you define your putters:

   procedure Put_Integer (Value : Integer) is
   begin
      iio.Put (Value);
   end Put_Integer;

   procedure Put_Float (Value : Float) is
   begin
      iii.Put (Value);
   end Put_Float;

And use them in the generic package's instantiations:

   package clist is new createlis 
     (size, integer, Put_Integer); use clist;
   -- ...
   package blist is new createlis
     (size, float, Put_Float); use blist;

You can't directly give the procedures in iio / iii as generic parameters as those take additional parameters with default values.

Upvotes: 3

Related Questions