Search code examples
classooprecordadasubtype

Using a Class/record in an other class (subtype mark required in this context)


I want to learn ADA and have a problem with the 'class'

I have 2 classes A and B and want that class A can use class B. Class A

with B;
package A is
   type A is tagged
      record
         varA : INTEGER := 0;
         varB : INTEGER := 0;
      end record;
   procedure DOSMTH(A_In : in out A; B_IN: in out B);

end A;

and Class B

package B is
   type B is tagged
      record
         var1 : INTEGER;
         var2 : INTEGER;
      end record;
      procedure DoSMTH2(B_IN: in out B; var  : INTEGER);
end B;

If I want to compile A I get following error message:

subtype mark required in this context
found 'B' declared at b.ads:1

Another problem is my 'Main' File which can't use A and B (declaration)

with Ada.Text_IO, A, B;
procedure Main is
   a : A;
   b : B;
begin
 null;  
end Main;

I get following Errors:

object "A" cannot be used before end of its declaration
object "B" cannot be used before end of its declaration

I can't find any solution for that. I have read the ADA 2005 Reference Manual but can't find anything.

OK thx for the help, finaly it works now.

Solution:

Modify Class A to

 with B;
    package A is
       type A is tagged
          record
             -- u can use  a object VAR with
             -- objB : B.B
             varA : INTEGER := 0;
             varB : INTEGER := 0;
          end record;
       procedure DOSMTH(A_In : in out A; B_IN: in out B.B);

    end A

Modify Main to:

with Ada.Text_IO, A, B;
procedure Main is
   myA : A.A; --the old one was stupid ...
   myB : B.B; --same here
begin
 null;  
end Main;

Solution

  • You only withed package B in package A - in order to use anything form it you have to use qualified name (that is: for record B you should use B.B). Like: with B; package A is type A is tagged record varA : INTEGER := 0; varB : INTEGER := 0; end record; procedure DOSMTH(A_In : in out A; B_IN: in out B.B); end A;

    If you want to use things declared in package B without fully qualifying name you should put use B; right after withing it.

    As to whether one should use or only with package is hard question - most of the time most Ada programmers that I know (including myself) prefer to with packages only, rarely using them - for example when working with Lumen library (OpenGL binding with glut-like functionality) I usually use Lumen because then I can type GL.Whatever_GL_Function instead of Lumen.GL.Whatever_GL_Function (but I do not "use" Lumen.GL). If package name is long I prefer to rename it inside procedure and in case of types (for operators if defined) one can use type B.B;

    PS: I can elaborate more if some parts of it are unclear.