我正在使用一个参数来修复所用类型的精度。
在我尝试在界面中使用相同的类型之前,这很好用。考虑这个小例子:
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'。
错误是
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) 但这一点都不优雅)
谢谢!
最佳答案
最简单的方法是添加 import
界面里面。模块的定义超出了接口(interface)的范围,这有点设计错误。平原 import
将导入所有内容。
....
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 - 从接口(interface)访问参数 (Fortran),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22976435/