从接口访问参数 (Fortran) [英] Access a parameter from an interface (Fortran)
问题描述
我正在使用一个参数来修复所用类型的精度.在我尝试在界面中使用相同的类型之前,这很好用.考虑这个小例子:
I am using a parameter to fix the precision of the used types. This works fine until I try to use the same type within an interface. Consider this small example:
module Hello
implicit none
save
integer, parameter :: K = selected_real_kind(10)
contains
subroutine dosomething(fun)
real(K) :: foo
interface
function fun(bar)
real(K) :: bar
real(K) :: fun
end function fun
end interface
end subroutine
end module
在这里,foo 将是所需的类型,而编译器 (gfortran) 会抱怨 'bar' 和 'fun'.
Here, foo would be of the desired type, whereas the compiler (gfortran) complains for 'bar' and 'fun'.
错误是
Error: Parameter 'k' at (1) has not been declared or is a variable, which does
not reduce to a constant expression
有没有办法让它工作?(目前,我只是到处写 selected_real_kind(10) 但这一点都不优雅)
Is there a way to get this working? (For now, I am just writing selected_real_kind(10) everywhere but this is not elegant at all)
谢谢!
推荐答案
最简单的方法是在界面中添加import
.模块的定义超出了接口的范围,这有点设计错误.普通的 import
将导入所有内容.
The easiest way is to add import
inside the interface. It is somewhat of a misdesign that the definitions of the module are outside the scope of the interface. Plain import
will import everything.
....
subroutine dosomething(fun)
real(K) :: foo
interface
function fun(bar)
import
real(K) :: bar
real(K) :: fun
end function fun
end interface
end subroutine
....
也可以:import :: K
这篇关于从接口访问参数 (Fortran)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!