简体   繁体   中英

Ada: accessing a record variable

Hi I am making a program to calculate vector components. Since 2D vectors have a horizontal and a vertical component in the Cartesian space, I have decided to use the record construction.

The program first calculates the basis vector and this is what is shown below. The horizontal , vertical components as well as an angle is asked as input. The angle refers to the anticlockwise positive angle from the default Cartesian coordinate system to another rotated Cartesian coordinate system from the original one.

In the file Projections.adb you will see the calculation:

D.Horz := A * Cos (C);

where A is the horizontal component of the original Cartesian coordinate system and C is the angle that denotes the rotation between the new Cartesian coordinate system and the old system.

In the main program Get_projections.adb , the calling procedure is

Vector_basis_r (Component_Horz, Component_Vert, theta, Basis_r);

Ada complains when I issue the command:

Ada.Long_Float_Text_IO.Put (Item => Basis_r.Horz, Fore => 3, Aft  => 3, Exp  => 0);

when I want to retrieve the horizontal component of the basis vector.

The complaint is:

*invalid prefix in selected component "Basis_r"*.

Any suggestions what I am doing wrong?

The necessary files are down here:

The main file is Get_Projections.adb :

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO;
with Ada.Float_Text_IO;
with Projections; Use Projections;
with Ada.Numerics.Long_Elementary_Functions;
use Ada.Numerics.Long_Elementary_Functions;


procedure Get_Projections is

   Component_Horz, Component_Vert, theta  : Long_Float;
   Basis_r                                : Rectangular;

begin

  Ada.Text_IO.Put("Enter the horizontal component ");
  Ada.Long_Float_Text_IO.Get (Item => Component_Horz);
  Ada.Text_IO.New_Line (1);
  Ada.Text_IO.Put("Enter the vertical component ");
  Ada.Long_Float_Text_IO.Get (Item => Component_Vert);
  Ada.Text_IO.New_Line (1);
  Ada.Text_IO.Put("Enter the angle ");
  Ada.Long_Float_Text_IO.Get (Item => theta);

  Vector_basis_r (Component_Horz, Component_Vert, theta, Basis_r);


  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put("rx = ");
  Ada.Long_Float_Text_IO.Put (Item => Basis_r.Horz, Fore => 3, Aft  => 3, Exp  => 0);

end Get_Projections;

and the accompanying packages are the specification Projections.ads :

package Projections is

   type Rectangular is private;
   procedure Vector_basis_r (A, B, C : in Long_Float; D : out Rectangular);

private
   type Rectangular is
        record
             Horz, Vert: Long_Float;
        end record;

end Projections;

and the package body Projections.adb :

with Ada.Numerics.Long_Elementary_Functions;
use  Ada.Numerics.Long_Elementary_Functions;

package body Projections is

   procedure Vector_basis_r (A, B, C : in Long_Float; D : out Rectangular) is
   begin
      D.Horz := A * Cos (C);
      D.Vert := B * Sin (c);
   end Vector_basis_r;

end Projections;

Thanks a lot...

Rectangular is a private type, therefore other packages do not have access to its components, Horz (or Vert) in this case. The fields of the Rectangular type can only be accessed by Projections' package body, or in the private part or bodies of any child packages of Projections.

Either place the Rectangular type declaration in the public part of package Projections, or provide Get/Set accessors to interact with the record's components.

I would put a put procedure in the projections package:

package Projections is

   type Rectangular is private;
   procedure Vector_basis_r (A, B, C : in Long_Float; D : out Rectangular);

   procedure put (r : in Rectangular);

private
   type Rectangular is
        record
             Horz, Vert: Long_Float;
        end record;

end Projections;

Then your privates stay hidden and you can print them with impunity.

put (basis_r);

and have as the body of the put:

procedure put (r : in Rectangular) is
begin 
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put("rx = ");
  Ada.Long_Float_Text_IO.Put (Item => r.Horz, Fore => 3, Aft  => 3, Exp  => 0);
end put;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM