我试图初始化一个记录变量,它本身包含一个使用聚合的嵌套记录,但似乎无法使语法正确.任何帮助赞赏.
with Step; use Step;
package Pattern is
-- ADT
type Pattern_Type is tagged private;
-- ADT components
type Bars_Type is private;
-- ADT instance methods
function Tempo (This: Pattern_Type) return Integer;
function Bars (This: Pattern_Type) return Bars_Type;
function Get_Next_Step(This: Pattern_Type ) return Step_Type;
-- Static methods
function Get_Basic_Beat return Pattern_Type;
private
type Bars_Type is range 0..2;
Number_Of_Steps : constant Natural := 32;
type Active_Step_Type is mod Number_Of_Steps;
type Steps_Type is array( Active_Step_Type ) of Step_Type;
type Pattern_Type is tagged record
Tempo : Integer range 40..400;
Bars : Bars_Type := 1;
Steps : Steps_Type;
Active_Step : Active_Step_Type := 1;
end record;
-- Package variable
Basic_Beat : Pattern_Type :=
( Tempo => 125,
Steps => Steps_Type'(1..31 => Step_Type'(Instrument => 'K', Velocity => 127, Offset => 0, Active => True)),
others => <> );
end Pattern;
Run Code Online (Sandbox Code Playgroud)
......并在steps.ads中
package Step is
-- ADT
type Step_Type is tagged private;
-- ADT instance methods
function Instrument (This : Step_Type) return Character;
function Velocity (This : Step_Type) return Integer;
function Offset (This : Step_Type) return Integer;
function Active (This : Step_type) return Boolean;
private
type Step_Type is tagged record
Instrument : Character := ' ';
Velocity : Integer := 0;
Offset : Integer := 0;
Active : Boolean := false;
end record;
end Step;
Run Code Online (Sandbox Code Playgroud)
我在GPS中遇到此构建错误
expected private type "Step_Type" defined at step.ads:4, found a composite type
我已尝试过Step_Type'...行的各种组合
根据您的需求,您有不同的选择:
#2的一个例子:
with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
package Step is
-- ADT
type Step_Type is tagged private;
function Make
(Instrument : Character;
Velocity : Integer;
Offset : Integer;
Active : Boolean)
return Step_Type;
private
type Step_Type is tagged record
Instrument : Character := ' ';
Velocity : Integer := 0;
Offset : Integer := 0;
Active : Boolean := false;
end record;
function Make
(Instrument : Character;
Velocity : Integer;
Offset : Integer;
Active : Boolean)
return Step_Type
is (Instrument => Instrument,
Velocity => Velocity,
Offset => Offset,
Active => Active);
end Step;
use Step;
package Pattern is
-- ADT
type Pattern_Type is tagged private;
-- ADT components
type Bars_Type is private;
private
type Bars_Type is range 0..2;
Number_Of_Steps : constant Natural := 32;
type Active_Step_Type is mod Number_Of_Steps;
type Steps_Type is array( Active_Step_Type ) of Step_Type;
type Pattern_Type is tagged record
Tempo : Integer range 40..400;
Bars : Bars_Type := 1;
Steps : Steps_Type;
Active_Step : Active_Step_Type := 1;
end record;
-- Package variable
Basic_Beat : Pattern_Type :=
( Tempo => 125,
Steps => (others => Make('K',127,0,True)),
others => <> );
end Pattern;
begin
Put_Line("Hello, world!");
end Hello;
Run Code Online (Sandbox Code Playgroud)