comp.lang.ada
 help / color / mirror / Atom feed
* Inheritance problem!
@ 2008-12-18 15:14 AndreiK
  2008-12-18 16:31 ` Adam Beneschan
  0 siblings, 1 reply; 13+ messages in thread
From: AndreiK @ 2008-12-18 15:14 UTC (permalink / raw)


Hello everybody!

I am quite new in Ada programming and I am trying to write simple (at
the moment) OO program, but I have a problem with inheritance of the
private type. I am using the GNAT+GPS (2008) and learning Ada by John
Barnes "Ada 2005".

The idea of the program is next: we have tagged private types Vector
and Matrix. The Matrix inheritances all components and functions from
Vector. Difference only in constructor: Mat is for Matrix and Vec is
for Vector.

In the test program the errors occures when I try to call inherited
function, in the example it is Cell(...) and if I try to use function
Put(This: Vect.Vector'Class).

I don't understand where the mistake is?

Thanks everybody for a help!

The code of the test program:

----------------------------------------
--   test.adb
----------------------------------------
with Ada.Text_IO;
with Vectors.Matrices;

procedure test is

   package TIO renames Ada.Text_IO;
   package FIO is new Ada.Text_IO.Float_IO(Float);

   package Vect is new Vectors(Float); --use Vect;
   package Matr is new Vect.Matrices;-- use Matr;
   M1: Matr.Matrix := Matr.Mat(4, 3, 1.0);
   V1: Vect.Vector := Vect.Vec(3, 2.0);

   --  Procedure, which intended to print out the Vector or Matrix
objects.
   --
   procedure Put(This: in Vect.Vector'Class) is
   begin
      TIO.New_Line;
      TIO.New_Line;
      for J in 0 .. This.Size(0)-1 loop
         TIO.Put("|");
         for K in 0 .. This.Size(1)-1 loop
            FIO.Put( Item => This.Cell(J, K),
                    Fore => 2,
                    Aft  => 3,
                    Exp  => 3);
            TIO.Put(" ");
         end loop;
         TIO.Put("|");
         TIO.New_Line;
      end loop;
      null;
   end;

begin


   FIO.Put(M1.Cell(0,0));
   -- test.adb:37:14: error:
   -- no selector "Cell" for private type "Matrix"
   -- defined at vectors-matrices.ads:6, instance at line 10

   Put(M1);
   -- test.adb:39:08: error: expected type "Vector'class"
   -- defined at vectors.ads:12, instance at line 9

   -- test.adb:39:08: error: found private type "Matrix"
   -- defined at vectors-matrices.ads:6, instance at line 10

   -- test.adb:39:08: error:   ==> in call to "Put" at line 14

   Put(V1);
   -- No Errors for Vector V1.

   FIO.Put(V1.Cell(0,0));
   -- No Errors for Vector V1.

   TIO.New_Line;

end test;

----------------------------------------
--  vectors.ads
----------------------------------------
generic
   type Real is digits <>;

package Vectors is
   pragma Preelaborate;

   subtype Dim_Type is Natural;
   type Sizes_Arr is array(Integer range <>) of Dim_Type;

   --
   --     The type <Vector> is private
   --
   type Vector is tagged private;
   pragma Preelaborable_Initialization (Vector);

   --
   --  Vec - the constructor of the entity of type Vector.
   --
   function Vec(Elem_Num: Dim_Type; Fill_by_Value: Real'Base := 0.0)
return Vector;

   --
   --  Function Cell returns the value of the element at position
Index
   --  of the object of the type Vector.
   --
   function Cell(
                 This : in Vector;
                 Row_Index: in Natural;
                 Col_Index: in Natural
                ) return Real'Base;

   function Row_Len(This: in Vector) return Natural;
   function Col_Len(This: in Vector) return Natural;

   --
   --  Function Size returns the array (1D) of the dimension's sizes
of the object
   --  of the type Vector.
   --
   function Size(This: in Vector) return Sizes_Arr;

private

   type Elements_Array is array (Natural range <>) of Real'Base;
   type Elements_Access is not null access Elements_Array;

   type Vector is tagged
      record
         Elements_ac        : Elements_Access;
         Count              : Natural := 0;
         Sizes              : Sizes_Arr(0..1);
         Transposed         : Boolean := False;
         Busy               : Natural := 0;
      end record;

end Vectors;

----------------------------------------
-- vectors-matrices.ads
----------------------------------------
generic

package Vectors.Matrices is
   pragma Preelaborate;

   type Matrix is tagged private;
   pragma Preelaborable_Initialization (Matrix);

   function Mat(Row_Num,Col_Num: in Dim_Type; Fill_by_Value:
Real'Base := 0.0) return Matrix;

private

   type Matrix is new Vector with null record;

end Vectors.Matrices;



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

end of thread, other threads:[~2008-12-23 13:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-18 15:14 Inheritance problem! AndreiK
2008-12-18 16:31 ` Adam Beneschan
2008-12-19 21:34   ` Maciej Sobczak
2008-12-19 22:37     ` Adam Beneschan
2008-12-20  0:02     ` Georg Bauhaus
2008-12-20  0:33       ` Jeffrey R. Carter
2008-12-20  8:28         ` Dmitry A. Kazakov
2008-12-21  1:18           ` Jeffrey R. Carter
2008-12-21  8:29             ` Dmitry A. Kazakov
2008-12-21 17:05               ` Jeffrey R. Carter
2008-12-22 17:10         ` hesobreira
2008-12-22 19:46           ` Jeffrey R. Carter
2008-12-23 13:06             ` Maciej Sobczak

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