开发者

Ada: float instantiation from another package

I'm trying to create a new Float type with 6 digits precision. But it seems that I'm not able to use it. Here's the code from the package MyFloat_IO.ads

WITH Ada.Text_IO;

PACKAGE MyFloat_IO IS

TYPE MyFloat IS DIGITS 6 RANGE 0.0..Float'Last;
PACKAGE MyFloat_IO IS NEW Ada.Text_IO.Float_IO(MyFloat);

end MyFloat_IO;

and the main code:

WITH Ada.Text_IO;
WITH MyFloat_IO;
USE MyFloat_IO;
WITH Ada.Numerics;


PROCEDURE TEST2 IS

X : MyFloat := 3.5;
Y : CONSTANT := Ada.Numerics.Pi;
开发者_运维问答Z : MyFloat;


BEGIN -- main program
  Z := X * Y;
  MyFloat_IO.Put(Z);
  Ada.Text_IO.New_Line;
END TEST2;

On compiling I get the error message:

14. BEGIN -- main program
15.     Z := X * Y;
16.     MyFloat_IO.Put(Z);
                  |
    >>> "Put" not declared in "MyFloat_IO"

17.     Ada.Text_IO.New_Line;
18. END TEST2;

What am I doing wrong?

Thanks a lot...


Update: new code as per the suggestions of T.E.D:

  1. package MyFloat_I0.ads :

    WITH Ada.Text_IO;
    PACKAGE MyFloat_I0 IS
    TYPE Instance IS DIGITS 6 RANGE 0.0..Float'Last;
    PACKAGE MyFloat IS NEW Ada.Text_IO.Float_IO(Instance);
    end MyFloat_I0;
    
  2. and the main code :

    WITH Ada.Text_IO;
    WITH MyFloat_I0;
    use MyFloat_I0;
    WITH Ada.Numerics;
    
    PROCEDURE TEST2 IS
    
    
    X : Instance := 3.5;
    Y : CONSTANT := Ada.Numerics.Pi;
    Z : Instance;
    
    
    BEGIN -- main program
      Z := X * Y;
      MyFloat.Instance.Put(Z);
      Ada.Text_IO.New_Line;
    END TEST2;
    

On compilation I get:

MyFloat.Instance.Put(Z);
               |
    >>> "Instance" not declared in "MyFloat"


What you are doing wrong is that you declared a package named MyFloat_IO (derived from Ada.Text_IO.Float_IO) inside of another package also named MyFloat_IO. In order to get at it the way you have things declared, you would have to call:

MyFloat_IO.MyFloat_IO.Put(Z);

I'm pretty sure it is possible to just derive a package from a generic by itself as a compilation unit. However, you might consider instead renaming the package MyFloat, the type something like Instance, and your IO package IO. That way, people looking to use it will say MyFloat.Instance for the type and MyFloat.IO for the IO package.

(Update answer)

I can see from the code you posted I must have confused you thoroughly. Here's what I had in mind (warning: not compiled).

package MyFloat is
   type Instance is digits 6 range 0.0..Float'Last; --'

   package IO is new Ada.Text_IO.Float_IO (Instance);
end MyFloat;

In other words, the package is named MyFloat, the type is named Instance, and the IO package is named IO. When called from outside the package, the type is MyFloat.Instance, and the put routine is MyFloat.IO.Put.

Note that if you are using Gnat, you will have to rename the source file when you rename the package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜