Search code examples
genericsstackrecordada

Push a record datatype into a stack


I am new to Ada and I can't figure out how to update a field in a record and push the record into a stack instantiated with a generic package.

I have tried using genericS.vehicleName and garageBay.vehicleName to no avail.

--Snippet
type vehicle is array(1..15) of character;
type vName is array(1..8) of character;
type garageBay is record
    vehicleType: vehicle;
    vehicleName: vName;
    time2Fix: integer;
    startTime: integer;
    finishTime: integer;
end record;
begin
get(lowerbound);
get(upperbound);
declare
package genericS is new gstack(lowerbound,upperbound, garageBay);
use genericS;
begin
    put("Enter vehicle name: " ); 
    get(garageBay.vehicleName); 
    tpush(garageBay);
end;

--Errors
x86_64-linux-gnu-gcc-8 -c gusestack.adb
gusestack.adb:24:21: invalid prefix in selected component "garageBay"
gusestack.adb:25:23: invalid use of subtype mark in expression or call
gnatmake: "gusestack.adb" compilation error

Solution

  • You appear to be confused between a type and an instance of a type. You define the type garbageBay but never create an instance of that type. A type declaration defines the structure of a type, including the amount of memory required for an instance of the type. It does not allocate memory for all imaginable instances of a type. You must create an instance of garbageBay in the declarative section of your code such as

    element : garbageBay;
    

    You can then modify your code to say

    get(element.vehicleName);
    tpush(element);