Chen Kinnrot
Chen Kinnrot

Reputation: 21015

ada syntax issues

I'm new to ada, I want to define a vector package and to be able to pass it to a method, where should I define the package, this is the code I need

package Document is new Ada.Containers.Vectors(Positive,Unbounded_String);
use Document;

I don't know where to put so it'll be visible to the main and another function file.

Upvotes: 1

Views: 236

Answers (3)

Simon Wright
Simon Wright

Reputation: 25491

If you are using GNAT (either the GPL version from AdaCore, or the FSF GCC one), you need a file document.ads (in the same working directory as you are going to put your main program and the other file).

Your new package Document needs to ‘with’ two other packages: Ada.Containers.Vectors and Ada.Strings.Unbounded.

You can’t put use Document; in document.ads; it needs to go in the packages that make use of Document by withing it. The use clause controls whether you need to write the fully qualified name - so, for example, to write Document as you have it you would say

with Ada.Containers.Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Document is new Ada.Containers.Vectors (Positive, Unbounded_String);

but it would be more conventional to write

with Ada.Containers.Vectors;
with Ada.Strings.Unbounded;
package Document is new Ada.Containers.Vectors
  (Positive,                                                
   Ada.Strings.Unbounded.Unbounded_String);

Your main program and other packages can now say with Document; (and, if you want, use Document;).

Upvotes: 5

user571138
user571138

Reputation:

In Addition to this, to actually declare the vector you would need to put in a declaritive section :

my_document : document.vector;

Then you can use the methods described in the vector package

Upvotes: 2

Rommudoh
Rommudoh

Reputation: 1804

Additional to the answer by Simon, you can put the two lines you stated anywhere into a declarative part. This can be inside a Subprogram like your main procedure, a library or anywhere else.

Example for main procedure:

with Ada.Containers.Vectors;
with Ada.Strings.Unbounded;
procedure My_Main is
   package Document is new Ada.Containers.Vectors
     (Positive,
      Ada.Strings.Unbounded.Unbounded_String);
   -- use it or declare other stuff...
begin
   -- something...
end My_Main;

To use it across multiple source files, put it into one of your packages or into a separate file like Simon wrote.

Upvotes: 3

Related Questions