没有选择器"加"类型" POLINOMIO"为什么?

时间:2015-03-16 03:20:16

标签: ada

这是关于的函数,polynomials_test.adb调用函数plus和polynomial.ads。但是还没有编译发送消息:不是选择器加上类型POLINOMIO。

为什么以及如何解决它? THX。

-------------- polynomial.ads --------------------

 --function plus
   --return c = a + b
    function plus( Poly1, Poly2: Polynomial ) return Polynomial is
        Poly_Sum : Polynomial := Zero_Polynomial;
   begin
        Poly_Sum.High_Power := Max( Poly1.High_Power, Poly2.High_Power );
        if Poly_Sum.High_Power > Max_Degree then
            raise Polynomial_Error;
        end if;
        for i in 0..Poly_Sum.High_Power loop
            Poly_Sum.Coeff_Array( i ) := Poly1.Coeff_Array( i ) + Poly2.Coeff_Array( i );
        end loop;

        return Poly_Sum;
   end plus;


-------------polynomials_test.adb----------------------
--libraries
with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Unchecked_Deallocation;
use Ada.Text_IO, Ada.Integer_Text_IO;

--package
with Polynomials; use Polynomials;

--main
procedure Polynomials_Test is

   --coef and deg
 type POLINOMIO is
      record
         coef    : INTEGER; -- this an array
         deg     : INTEGER;
      end record;


   poli_a : polinomial := (others => 1 );

   type ACCESS_MY_DATA is access POLINOMIO;

   --Unchecked_Deallocation: delete an object from the heap
   procedure Free is new
                Ada.Unchecked_Deallocation(POLINOMIO, ACCESS_MY_DATA);

    --definitions
     &   : ACCESS_MY_DATA;
   Zero  : ACCESS_MY_DATA := new POLINOMIO'(0,0);
     p1  : ACCESS_MY_DATA := new POLINOMIO'(4,3);
     p2  : ACCESS_MY_DATA := new POLINOMIO'(3,2);
     p3  : ACCESS_MY_DATA := new POLINOMIO'(1,0);
     p4  : ACCESS_MY_DATA := new POLINOMIO'(2,1);
     p   : ACCESS_MY_DATA := new POLINOMIO'(4,3);
     q1  : ACCESS_MY_DATA := new POLINOMIO'(3,2);
     q2  : ACCESS_MY_DATA := new POLINOMIO'(1,0);
     q   : ACCESS_MY_DATA := new POLINOMIO'(2,1);
     r   : ACCESS_MY_DATA := new POLINOMIO'(3,2);
     s   : ACCESS_MY_DATA := new POLINOMIO'(1,0);
     t   : ACCESS_MY_DATA := new POLINOMIO'(2,1);
     p5  : ACCESS_MY_DATA := new POLINOMIO'(0,1);

     pa : Poly := new Polynomial;
     pb : Poly := new Polynomial;

begin

   --asignation and initialization
    & := new POLINOMIO;
   Zero := new POLINOMIO;
   p1 := new POLINOMIO;
   p2 := new POLINOMIO;
   p3 := new POLINOMIO;
   p4 := new POLINOMIO;
   p  := new POLINOMIO;

   p5 := new POLINOMIO;

   q1 := new POLINOMIO;
   q2 := new POLINOMIO;
   q  := new POLINOMIO;

   r := new POLINOMIO;
   s := new POLINOMIO;
   t := new POLINOMIO;


   --polynomial  functions
   Zero := new POLINOMIO'(0,0);
   p1 := new POLINOMIO'(4,3);
   p2 := new POLINOMIO'(3,2);
   p3 := new POLINOMIO'(1,0);
   p4 := new POLINOMIO'(2,1);
   p  := new POLINOMIO'(4,3);
   p := p1 & p2 & p3 & p4;

   --p:= p1.plus_2(p2).plus_2(p3).plus_2(p4);

   q1 := new POLINOMIO'(3,2);
   q2 := new POLINOMIO'(5,0);
   q := q1.plus(q2);

   --q  := q1.plus(q2);

   Put_Line("Polynoms:");
   put("zero(x) =   " & Integer'Image(p3.deg));
   New_Line;
   -- p(x) =        4x^3 + 3x^2 + 2x + 1
   put("p(x) =      " & Integer'Image(p));

end Polynomials_Test;

1 个答案:

答案 0 :(得分:3)

这些子程序(函数和过程)可能不是您想要的那样:它们在Polynomials包中声明,但根本不引用类型Polynomial

除此之外,您的问题很容易回答:POLYNOMIO不是标记类型,因此object.function表示法不起作用。你应该改为调用type.function,即Polynomials.times (q)

我猜你正在使用C ++或Java等语言的经验(或一些预先编写的代码)。在那里,函数(方法)属于类/类型,并将隐式参数带到该类的实例。 Ada不是这种情况:函数属于,您必须自己添加一个参数,例如:

function times (p: Polynomial; b: Integer) return Integer;

(难道它不会返回多项式吗?)

一个问题:为什么要声明两种类似的类型,Polynomials.PolynomialPolynomials_Test.POLYNOMIO