Ada:声明部分、函数和包
我有一个关于声明部分、函数和包的查询。我有一个如下所示的包。由于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,编译器正在准确地告诉您问题所在:-)
您正尝试在仅允许声明的代码区域中执行赋值语句。
至少我没有看到你的 X1 声明(尽管这可能是因为你没有在上面的代码中摘录它),我也没有看到分配给 Force_Applied 和 Forcing_Frequency 的声明和值。
可以修复这个问题,使其编译接近原样:
或者,通过向包体添加初始化块:
但老实说,我真的不认为这两者都是您真正想要的。
在您的主程序中,您可以只声明 X1 (以及作为参数传递的变量),然后调用您的计算函数,例如(警告,未编译)
这是获取您可能正在寻找的内容的一种方法。
或者,如果 X1 仅在 Compute_J21 中使用,您应该调用 Compute_X1 作为该函数中执行的第一个语句之一(或作为 X1 声明的初始化)(并将 Force_Applied 而不是 X1 作为参数传递给 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:
or, by adding an initialization block to the package body:
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)
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):
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.