Ada:声明部分、函数和包

发布于 2024-09-24 07:17:49 字数 1203 浏览 5 评论 0原文

我有一个关于声明部分、函数和包的查询。我有一个如下所示的包。由于 Compute_X1 函数的复杂性,我创建了一个“独立”来计算该函数。从 Compute_X1 返回的值是 X1,并将在函数 J21 中使用(J21 将 X1 作为第一个参数)。

包规范:

package Compute_Jacobian is

--compute X1
function Compute_X1 ( Force_Applied, Forcing_Frequency: Long_Float) return Long_Float;

--- use X1 in J21
function J21 ( X1, Forcing_Frequency, Natural_Frequency : Long_Float) return Long_Float;

end Compute_Jacobian;

包主体是:

package body Compute_Jacobian is
 --compute X1

function Compute_X1 ( Force_Applied, Forcing_Frequency: Long_Float) return Long_Float is separate;

X1 := Compute_X1 ( Force_Applied, Forcing_Frequency);


function J21 ( X1, Forcing_Frequency, Natural_Frequency : Long_Float) return Long_Float is separate;

end Compute_Jacobian;

我已经为 Compute_X1 和 J21 创建了存根。

在编译包体 Compute_Jacobian.adb 时,我收到以下错误消息:

12.    X1 := Compute_X1 ( Force_Applied, Forcing_Frequency);
       |
    >>> statement not allowed in declarative part

我的问题是如何计算 X1 并将其用于函数 J21 的计算。

我可以尝试直接在“主”代码(此处未显示)中计算 X1(与那里形成“独立”),然后将其用作计算 J21 时的普通参数。但我想要上述结构(在我上面的帖子中)以及 Compute_Jacobian 包中 X1 的计算。

多谢...

I have a query with declarative part, function and packages. I have a package as shown next. Due to the complexity of the Compute_X1 function, I have create a "is separate" for computing this function. The value returned from Compute_X1 is X1 and is to be used in the function J21 (J21 takes X1 as a first argument).

Package specification:

package Compute_Jacobian is

--compute X1
function Compute_X1 ( Force_Applied, Forcing_Frequency: Long_Float) return Long_Float;

--- use X1 in J21
function J21 ( X1, Forcing_Frequency, Natural_Frequency : Long_Float) return Long_Float;

end Compute_Jacobian;

The package body is:

package body Compute_Jacobian is
 --compute X1

function Compute_X1 ( Force_Applied, Forcing_Frequency: Long_Float) return Long_Float is separate;

X1 := Compute_X1 ( Force_Applied, Forcing_Frequency);


function J21 ( X1, Forcing_Frequency, Natural_Frequency : Long_Float) return Long_Float is separate;

end Compute_Jacobian;

and I have created stubs for Compute_X1 and J21.

On compiling the package body Compute_Jacobian.adb, I get this error message:

12.    X1 := Compute_X1 ( Force_Applied, Forcing_Frequency);
       |
    >>> statement not allowed in declarative part

My question is how to compute X1 and use it in the computation of function J21.

I could try to compute X1 in the "main" code (not shown here) directly (making a "is separate" from there) and then use it as a normal argument in computing J21. But I wanted the above structure (in my above post here) with computation of X1 in the Compute_Jacobian package.

Thanks a lot...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2024-10-01 07:17:49

好吧,编译器正在准确地告诉您问题所在:-)

您正尝试在仅允许声明的代码区域中执行赋值语句。

至少我没有看到你的 X1 声明(尽管这可能是因为你没有在上面的代码中摘录它),我也没有看到分配给 Force_Applied 和 Forcing_Frequency 的声明和值。

可以修复这个问题,使其编译接近原样:

X1 : Long_Float := Compute_X1 ( Force_Applied, Forcing_Frequency);

或者,通过向包体添加初始化块:

package body Computer_Jacobian is
    ...
    X1 : Long_Float;
    ...
begin
   X1 := Compute_X1 ( Force_Applied, Forcing_Frequency);
end Compute_Jacobian;

但老实说,我真的不认为这两者都是您真正想要的。

在您的主程序中,您可以只声明 X1 (以及作为参数传递的变量),然后调用您的计算函数,例如(警告,未编译)

with Compute_Jacobian;
procedure Do_Computations is

  Force_Applied     : Long_Float := 1.0;
  Forcing_Frequency : Long_Float := 10.0;
  Natural_Frequency : Long_Float := 5.0;

  X1  : Long_Float;
  J21 : Long_Float;

begin
   X1  := Compute_Jacobian.Compute_X1 (Force_Applied, Forcing_Frequency);
   J21 := Compute_Jacobian.Compute_J21 (X1, Forcing_Frequency, Natural_Frequency);
end Do_Computations;

这是获取您可能正在寻找的内容的一种方法。

或者,如果 X1 仅在 Compute_J21 中使用,您应该调用 Compute_X1 作为该函数中执行的第一个语句之一(或作为 X1 声明的初始化)(并将 Force_Applied 而不是 X1 作为参数传递给 Compute_J21):

function Compute_J21 (Force_Applied, 
                      Forcing_Frequency,
                      Natural_Frequency : Long_Float)
                                   return Long_Float is

   -- Declarations...
   X1 : Long_Float := Compute_X1(Force_Applied, Forcing_Frequency);

begin
   -- Computations that utilize X1...

end Compute_J21;

关于“是单独的”的使用的一个注释...我理解由于它们的复杂性而将这些函数实现分开的冲动,但我仍然建议不要这样做。它只是添加另一个文件来跟踪,如果您使用 GNAT,您不会通过尝试单独编译它们来获得任何性能增益,因为整个包体最终都会被处理,并且现在使用“是单独的”在主流 Ada 编程中相当罕见。这个答案涵盖了其中的一些内容在您之前提出的问题中。

Well, the compiler is telling you exactly what the problem is :-)

You're trying to execute an assignment statement in a region of the code that permits only declarations.

At the very least I don't see your declaration of X1 (although that may be because you don't excerpt it in the above code), nor do I see declarations and values being assigned to Force_Applied and Forcing_Frequency.

This can be fixed up so that it will compile close to as-is:

X1 : Long_Float := Compute_X1 ( Force_Applied, Forcing_Frequency);

or, by adding an initialization block to the package body:

package body Computer_Jacobian is
    ...
    X1 : Long_Float;
    ...
begin
   X1 := Compute_X1 ( Force_Applied, Forcing_Frequency);
end Compute_Jacobian;

But honestly, I really don't think either of these is what you really want.

In your main program you could just declare X1 (and the variables being passed as arguments) and then invoke your Compute functions, e.g. (warning, not compiled)

with Compute_Jacobian;
procedure Do_Computations is

  Force_Applied     : Long_Float := 1.0;
  Forcing_Frequency : Long_Float := 10.0;
  Natural_Frequency : Long_Float := 5.0;

  X1  : Long_Float;
  J21 : Long_Float;

begin
   X1  := Compute_Jacobian.Compute_X1 (Force_Applied, Forcing_Frequency);
   J21 := Compute_Jacobian.Compute_J21 (X1, Forcing_Frequency, Natural_Frequency);
end Do_Computations;

This is one approach to getting what you might be looking for.

Or if X1 is used solely within Compute_J21, you should just invoke Compute_X1 as one of the first statements executed (or as an initialization of the X1 declaration) within that function (and pass Force_Applied, instead of X1, as a argument to Compute_J21):

function Compute_J21 (Force_Applied, 
                      Forcing_Frequency,
                      Natural_Frequency : Long_Float)
                                   return Long_Float is

   -- Declarations...
   X1 : Long_Float := Compute_X1(Force_Applied, Forcing_Frequency);

begin
   -- Computations that utilize X1...

end Compute_J21;

And one note about the use of "is separate"... I understand the urge to set those function implementations apart because of their complexity, but I would still recommend against doing so. It simply adds another file to keep track of, if you're using GNAT you don't get any performance gain by just trying to compile them separately since the whole package body ends up getting processed anyway, and using "is separate" is now pretty uncommon in mainstream Ada programming. This answer covered some of this in a previous question you'd asked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文