从界面访问参数(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屋!