I've a small package file where I just want to define an array of couple of integer, but I want to protect it by making the type private. But after compiling and debugging I get a compilation error that I cannot solve.
Here is my code:
package Objet_Packing is
type Objet_Type is private;
subtype Objet is private; // This is line 9
type Tableau_Objet (<>) is private;
private // This is line 13
type Objet_Type is record
Largeur : Integer;
Hauteur : Integer;
end record;
subtype Objet is Objet_Type;
type Tableau_Objet is array ( Integer range <> ) of Objet;
end Objet_Packing;
And here is the error I get:
gcc-4.6 -c test_objet_packing.adb
objet_packing.ads:9:20: subtype indication expected
objet_packing.ads:13:01: only one private part allowed per package
gnatmake: "test_objet_packing.adb" compilation error
So I don't understand those two messages, and a few help would be great.
You can't have a private subtype.
To declare something as a subtype, you have to specify the base type that it's a subtype of. You can't expose the fact that it's a subtype while hiding the base type.
Either declare it visibly as a subtype of Objet_Type
:
subtype Objet is Objet_Type;
or declare it as a private type:
type Objet is private;
In the latter case, you can define it as a derived type in the private part:
type Objet is new Objet_Type;
which means you might need to add conversions in some places. Or you can put the derived type declaration in the visible part.
On the other hand, it's not at all clear why you need two names (Objet_Type
and Objet
) for what are essentially the same type. What distinction are you trying to make? Perhaps a better alternative is to remove the declaration of Objet
altogether, and just use Objet_Type
itself directly. (Perhaps Objet
would be a better name than Objet_Type
, but that's a style issue.)
As Brian Drummond's comment says:
The second error is just a spurious consequence of the parser's failure to understand the first error ... seeing the word "private" twice
Here's a modified version your package spec that declares only a single private record type. It compiles without error.
package Objet_Packing is
type Objet is private;
type Tableau_Objet (<>) is private;
private
type Objet is record
Largeur : Integer;
Hauteur : Integer;
end record;
type Tableau_Objet is array ( Integer range <> ) of Objet;
end Objet_Packing;
Though since the name Tableau_Objet
implies that it's an array of Objet
anyway, I"m not sure why you're hiding that behind a private type (and frankly it's been long enough since I've really used Ada that I'm not entirely sure how you'd use the type Tableau_Objet
). A more transparent alternative:
package Objet_Packing is
type Objet is private;
type Tableau_Objet is array ( Integer range <> ) of Objet;
private
type Objet is record
Largeur : Integer;
Hauteur : Integer;
end record;
end Objet_Packing;