2

I have an assignment in Ada which requires that I pass multiple arrays to a procedure inside a separate package from my main code. I have searched around, but the “solutions” don’t seem to address my situation and have left me even more confused.

I have seen examples where you have to define the array as a type (TYPE IntArray IS ARRAY (Integer RANGE <>) OF Integer), but that seems to only work when passing from one procedure to another within the same package - how can I share the array between packages?

    --multistack.ads
        GENERIC
...
           TYPE Item IS PRIVATE;
           WITH PROCEDURE Put(Job: IN Item);
           PACKAGE MultiStack IS
...
              TYPE IntArray IS ARRAY (Integer RANGE <>) OF Integer;      
              TYPE StackArray IS ARRAY (Integer RANGE <>) OF Item;

              Base:IntArray(1..NumberOfStacks);
...
           END MultiStack;

and

--stackmove.ads
    WITH MultiStack;
        GENERIC
          TYPE Item IS PRIVATE;
          PACKAGE StackMove IS
             PROCEDURE ReallocateArray(Base:IN OUT MultiStack.IntArray);
     END StackMove;

I end up with error stackmove.ads:13:160: invalid prefix in selected component "MultiStack"

3
  • Why doesn't it work? If you're getting an error, please post some code and let us know what error you're getting.
    – ajb
    Apr 3, 2014 at 23:08
  • I dont know what kind of college class you are taking but if your curriculum requires you to work in Ada in the future, I strongly suggest you read the book by Barnes.
    – Rick
    Apr 3, 2014 at 23:41
  • The error message you show in the last paragraph of the question refers to line 13 column 160, but the code you show has only 7 lines and meny fewer than 160 columns. Apr 11, 2014 at 17:56

1 Answer 1

1

If you define a type Int_Array in the specification (visible part) of package Package_1, and you want to refer to that type in package Package_2, you need to use with clauses to make the type name visible:

with Package_1;
package Package_2 is

    procedure Something (A : Package_1.Int_Array);

    -- .........

end Package_2;

or

with Package_1;  use Package_1;
package Package_2 is

    procedure Something (A : Int_Array);

    -- .........

end Package_2;

with Package_1 is needed to make anything defined in Package_1 available. Even then, you have to use a qualifier Package_1. in front of any identifier defined there, to let the compiler know that's where to find it. use Package_1 means you can use the names defined in Package_1 without that qualifier. However, using use can make it more difficult for a reader to tell where the names are coming from, especially if you use too many packages. Different Ada programmers have different opinions about whether to use use and when.

If this is not the issue, please post more information.

MORE: OK, since your packages are generic, you have a rather different problem.

The generic package MultiStack doesn't actually define a type IntArray that you can use. The only way you can use it is to instantiate the generic. If you define Inst to be an instance of the generic, like this:

package Inst is new MultiStack (Item => Some_Type, Put => Some_Put_Procedure);

then you can refer to Inst.IntArray. But MultiStack.IntArray is never going to be legal outside of MultiStack itself.

So to get this to work with StackMove, there are two possibilities, and you'll need to decide depending on what your needs are: (1) There will be a specific instance My_Multistack that you've already instantiated, that StackMove will work with; (2) more likely, you'd like StackMove to work with any instance of MultiStack. If (1) is correct, then just change MultiStack to My_MultiStack in stackmove.ads. But if you want (2), you'll need a generic formal package:

with MultiStack;
generic
    type Item is private;
    with package Stack_Instance is new MultiStack(<>);
package StackMove is
    procedure ReallocateArray (Base : in out Stack_Instance.IntArray);
end StackMove;

(You can put Item and Stack_Instance in either order.)

Doing the above won't guarantee that the Stack_Instance was instantiated with the same Item as StackMove. If you want to guarantee that:

with MultiStack;
generic
    type Item is private;
    with package Stack_Instance is new MultiStack(Item => Item, Put => <>);
package StackMove is
    procedure ReallocateArray (Base : in out Stack_Instance.IntArray);
end StackMove;

Then StackMove will work only with a MultiStack that was instantiated with the same Item; the good news is that now the compiler can be certain that the two Item types are the same, so you won't get type conflicts.

To instantiate, using the same Inst example as above:

package Inst is new MultiStack (Item => Some_Type, Put => Some_Put_Procedure);
package StackMove_Inst is new StackMove (Item => Some_Type, Stack_Instance => Inst);

[The above two don't have to be together like that; they can be in different packages.]

2
  • I tried something similar to this but still get invalid prefix in selected component "MultiStack". I have updated my question.
    – DaWood
    Apr 3, 2014 at 23:54
  • @DaWood You didn't say it was generic--that changes a lot of things.
    – ajb
    Apr 4, 2014 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.