comp.lang.ada
 help / color / mirror / Atom feed
* Ada 2005 problem using Iterate
@ 2016-10-05  7:28 Roger
  2016-10-05  8:15 ` Alejandro R. Mosteo
  0 siblings, 1 reply; 4+ messages in thread
From: Roger @ 2016-10-05  7:28 UTC (permalink / raw)


I've successfully used Iterate quite often before but the attached code won't work and I can't figure out why.
Presumably something obvious that I'm blind to!
No matter what I try, compiling numbers.adb produces the error messages:

numbers.adb:24:48: expected access to subprogram "Ada.Containers.Vectors." from instance at numbers.ads:17
numbers.adb:24:48: found type access to procedure "Show_Two_Numbers" defined at line 24

numbers.ads:17 is:
     package pNumbers_Vector is new  Ada.Containers.Vectors (tNumbers_Index, tNumbers);
numbers.adb:24 is:
     Numbers_Vector.Iterate(Show_Two_Numbers'Access);

Can someone tell me what I'm doing wrong?
Regards,
Roger

Numbers.ads:
with Ada.Containers.Vectors;
package Numbers is
    type tNumbers_Vector is private;
    procedure Print_Numbers_Data  (Numbers_Vector : tNumbers_Vector);
private
   type tNumbers is record
      n1                    : Integer := 0;
      n2                    : Integer := 0;
   end record;

 subtype tNumbers_Index is Natural;
 package pNumbers_Vector is new  Ada.Containers.Vectors (tNumbers_Index, tNumbers);
 type tNumbers_Vector is new pNumbers_Vector.Vector with null record;
end Numbers;

============================================================

Numbers.adb:
with Ada.Text_IO; use Ada.Text_IO;

package body Numbers is
    Numbers_List : tNumbers_Vector;
    function  New_Record(n1 : Integer; n2 : Integer) return tnumbers is
        theRecord : tnumbers := (n1, n2);
    begin
        return theRecord;
    end New_Record;

    --  ----------------------------------------------------------------------------------------------------

    procedure Print_Numbers_Data  (Numbers_Vector : tNumbers_Vector) is

        procedure Show_Two_Numbers(Number_Index : tNumbers_Index) is
             aNumber_Record    : tnumbers;
        begin
             aNumber_Record := Numbers_Vector.Element(Number_Index);
             Put("Number 1: " & Integer'image(aNumber_Record.n1));
             Put_Line("      Number 2: " & Integer'image(aNumber_Record.n2));
         end Show_Two_Numbers;

  begin
        Numbers_Vector.Iterate(Show_Two_Numbers'Access);
  end Print_Numbers_Data;

    --  ----------------------------------------------------------------------------------------------------

begin
    for index in Natural range 0..3 loop
        Numbers_List.Append(New_Record(index + 2, 2* index + 1));
    end loop;
end Numbers;


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada 2005 problem using Iterate
  2016-10-05  7:28 Ada 2005 problem using Iterate Roger
@ 2016-10-05  8:15 ` Alejandro R. Mosteo
  2016-10-05 23:27   ` Roger
  0 siblings, 1 reply; 4+ messages in thread
From: Alejandro R. Mosteo @ 2016-10-05  8:15 UTC (permalink / raw)


On 05/10/16 09:28, Roger wrote:
> I've successfully used Iterate quite often before but the attached code won't work and I can't figure out why.
> Presumably something obvious that I'm blind to!

What I see from checking the a-convec.ads spec

procedure Iterate
      (Container : Vector;
       Process   : not null access procedure (Position : Cursor));

is that your Show_Two_Numbers should expect a Cursor and not and index. 
I don't see any other iterate with a compatible profile.

HTH,
Alex.

> No matter what I try, compiling numbers.adb produces the error messages:
>
> numbers.adb:24:48: expected access to subprogram "Ada.Containers.Vectors." from instance at numbers.ads:17
> numbers.adb:24:48: found type access to procedure "Show_Two_Numbers" defined at line 24
>
> numbers.ads:17 is:
>      package pNumbers_Vector is new  Ada.Containers.Vectors (tNumbers_Index, tNumbers);
> numbers.adb:24 is:
>      Numbers_Vector.Iterate(Show_Two_Numbers'Access);
>
> Can someone tell me what I'm doing wrong?
> Regards,
> Roger
>
> Numbers.ads:
> with Ada.Containers.Vectors;
> package Numbers is
>     type tNumbers_Vector is private;
>     procedure Print_Numbers_Data  (Numbers_Vector : tNumbers_Vector);
> private
>    type tNumbers is record
>       n1                    : Integer := 0;
>       n2                    : Integer := 0;
>    end record;
>
>  subtype tNumbers_Index is Natural;
>  package pNumbers_Vector is new  Ada.Containers.Vectors (tNumbers_Index, tNumbers);
>  type tNumbers_Vector is new pNumbers_Vector.Vector with null record;
> end Numbers;
>
> ============================================================
>
> Numbers.adb:
> with Ada.Text_IO; use Ada.Text_IO;
>
> package body Numbers is
>     Numbers_List : tNumbers_Vector;
>     function  New_Record(n1 : Integer; n2 : Integer) return tnumbers is
>         theRecord : tnumbers := (n1, n2);
>     begin
>         return theRecord;
>     end New_Record;
>
>     --  ----------------------------------------------------------------------------------------------------
>
>     procedure Print_Numbers_Data  (Numbers_Vector : tNumbers_Vector) is
>
>         procedure Show_Two_Numbers(Number_Index : tNumbers_Index) is
>              aNumber_Record    : tnumbers;
>         begin
>              aNumber_Record := Numbers_Vector.Element(Number_Index);
>              Put("Number 1: " & Integer'image(aNumber_Record.n1));
>              Put_Line("      Number 2: " & Integer'image(aNumber_Record.n2));
>          end Show_Two_Numbers;
>
>   begin
>         Numbers_Vector.Iterate(Show_Two_Numbers'Access);
>   end Print_Numbers_Data;
>
>     --  ----------------------------------------------------------------------------------------------------
>
> begin
>     for index in Natural range 0..3 loop
>         Numbers_List.Append(New_Record(index + 2, 2* index + 1));
>     end loop;
> end Numbers;
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada 2005 problem using Iterate
  2016-10-05  8:15 ` Alejandro R. Mosteo
@ 2016-10-05 23:27   ` Roger
  2016-10-06  7:24     ` Alejandro R. Mosteo
  0 siblings, 1 reply; 4+ messages in thread
From: Roger @ 2016-10-05 23:27 UTC (permalink / raw)


Thanks Alex,
As expected, a simple reason!

Code that works:

with Ada.Text_IO; use Ada.Text_IO;

package body Numbers is
    Numbers_List : tNumbers_Vector;
    function  New_Record(n1 : Integer; n2 : Integer) return tnumbers is
        theRecord : tnumbers := (n1, n2);
    begin
        return theRecord;
    end New_Record;

    --  ----------------------------------------------------------------------------------------------------

    procedure Print_Numbers_Data  (Numbers_Vector : tNumbers_Vector) is

        procedure Show_Two_Numbers(Number_Cursor : pNumbers_Vector.Cursor) is
             aNumber_Record    : tnumbers;
        begin
             aNumber_Record := Numbers_Vector.Element(pNumbers_Vector.To_Index(Number_Cursor));
             Put("Number 1: " & Integer'image(aNumber_Record.n1));
             Put_Line("      Number 2: " & Integer'image(aNumber_Record.n2));
         end Show_Two_Numbers;

    begin
        Numbers_Vector.Iterate(Show_Two_Numbers'Access);
    end Print_Numbers_Data;

    --  ----------------------------------------------------------------------------------------------------
    
 procedure Load_Numbers(Numbers_Vector : in out tNumbers_Vector) is
 begin
    for index in Natural range 0..3 loop
        Numbers_Vector.Append(New_Record(index + 2, 2* index + 1));
    end loop;
 end Load_Numbers;

end Numbers;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada 2005 problem using Iterate
  2016-10-05 23:27   ` Roger
@ 2016-10-06  7:24     ` Alejandro R. Mosteo
  0 siblings, 0 replies; 4+ messages in thread
From: Alejandro R. Mosteo @ 2016-10-06  7:24 UTC (permalink / raw)


On 06/10/16 01:27, Roger wrote:
> Thanks Alex,
> As expected, a simple reason!

Tunnel vision happens ;)

Can't you use Element directly on the cursor?

Alex.

>
> Code that works:
>
> with Ada.Text_IO; use Ada.Text_IO;
>
> package body Numbers is
>     Numbers_List : tNumbers_Vector;
>     function  New_Record(n1 : Integer; n2 : Integer) return tnumbers is
>         theRecord : tnumbers := (n1, n2);
>     begin
>         return theRecord;
>     end New_Record;
>
>     --  ----------------------------------------------------------------------------------------------------
>
>     procedure Print_Numbers_Data  (Numbers_Vector : tNumbers_Vector) is
>
>         procedure Show_Two_Numbers(Number_Cursor : pNumbers_Vector.Cursor) is
>              aNumber_Record    : tnumbers;
>         begin
>              aNumber_Record := Numbers_Vector.Element(pNumbers_Vector.To_Index(Number_Cursor));
>              Put("Number 1: " & Integer'image(aNumber_Record.n1));
>              Put_Line("      Number 2: " & Integer'image(aNumber_Record.n2));
>          end Show_Two_Numbers;
>
>     begin
>         Numbers_Vector.Iterate(Show_Two_Numbers'Access);
>     end Print_Numbers_Data;
>
>     --  ----------------------------------------------------------------------------------------------------
>
>  procedure Load_Numbers(Numbers_Vector : in out tNumbers_Vector) is
>  begin
>     for index in Natural range 0..3 loop
>         Numbers_Vector.Append(New_Record(index + 2, 2* index + 1));
>     end loop;
>  end Load_Numbers;
>
> end Numbers;
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-10-06  7:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-05  7:28 Ada 2005 problem using Iterate Roger
2016-10-05  8:15 ` Alejandro R. Mosteo
2016-10-05 23:27   ` Roger
2016-10-06  7:24     ` Alejandro R. Mosteo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox