2012-05-16 28 views
1

我是新來的ADA,我要定義一個矢量包,並能夠將它傳遞給方法,我應該在哪裏定義的包,這是我的代碼需要ADA語法問題

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

我不知道該放在哪裏,所以它可以在主函數文件和其他函數文件中看到。

回答

6

如果您使用的是GNAT(或者是GPL version from AdaCore或FSF GCC),您需要一個文件document.ads(與您要放置主程序和其他文件的工作目錄相同)。

您的新包Document需要'with'兩個其他包:Ada.Containers.VectorsAda.Strings.Unbounded

你不能把use Document;放在document.ads;它需要在使用Document的軟件包中使用with。該use子句控制你是否需要寫全名 - 因此,例如,寫Document因爲你擁有它,你會說

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

,但它會更傳統的寫

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

您的主程序和其他程序包現在可以說with Document;(並且,如果您願意,use Document;)。

3

除了Simon的回答外,您可以將您在任何地方陳述的兩行寫入declarative part。這可以在一個子程序中,如主程序,庫或其他任何地方。

舉例主要步驟:

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; 

要使用它在多個源文件,把它放到你的包中的一個或到一個單獨的文件中像西蒙寫道。

2

除了this,實際聲明則需要放在一個declaritive部分的載體:

my_document : document.vector; 

然後你可以使用在載體中描述的方法package