从接口(Fortran)访问参数
我正在使用一个参数来修复所用类型的精度。 在我尝试在接口中使用相同的类型之前,这一切都很正常。考虑这个小例子:从接口(Fortran)访问参数,fortran,Fortran,我正在使用一个参数来修复所用类型的精度。 在我尝试在接口中使用相同的类型之前,这一切都很正常。考虑这个小例子: module Hello implicit none save integer, parameter :: K = selected_real_kind(10) contains subroutine dosomething(fun) real(K) :: foo interface
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
有没有办法让它工作起来?
(目前,我只是到处写精选的真实类(10),但这一点也不优雅)
谢谢大家! 最简单的方法是在界面内部添加
import
。模块的定义超出了接口的范围,这有点设计错误。普通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
接口块的主要原因之一是为“在别处定义”的过程(如外部过程)提供接口。其他地方定义的过程可能不知道在接口块的主机中定义的内容(它们可能不具有相同的主机),因此将自动主机关联排除在接口体中对我来说似乎是合理的。