从接口访问参数(Fortran)

时间:2014-04-10 00:27:02

标签: fortran

我正在使用参数来修复已使用类型的精度。 这工作正常,直到我尝试在界面中使用相同的类型。考虑这个小例子:

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'和有趣的'。

错误是

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),但这根本不优雅)

谢谢!

1 个答案:

答案 0 :(得分:6)

最简单的方法是在界面中添加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