Question

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;
Was it helpful?

Solution 2

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.

OTHER TIPS

The problem with the main program is that Ada isn’t case-sensitive, so when you say

a : A.A;

the compiler sees as far as a : A and thinks that the second A is the same thing as the first, hence the complaint that object "A" cannot be used before end of its declaration.

You can tell the compiler where to look for the package A by qualifying it. Package A is declared at library level, and there’s a notional package Standard you can use to indicate this:

a : Standard.A.A;

However, it’s much better, if you can, to avoid using the same name for different things in the first place.

My guess would be that the compiler sees "B" as the name of the package and not the record. Try naming your package or records differently.

Also, if you want to learn Ada, the reference manual is the last place you want to start at. Get the book by Barnes or get one of the older books used at Amazon. You get good books about Ada 95 for 5$ in the used section.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top