fortran - 从接口(interface)访问参数 (Fortran)

标签 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' 和 '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/

相关文章:

pointers - 通过类型/种类/等级以外的方式区分 Fortran 中的泛型

fortran - OpenMP 工作精度

grid - 算法[最好是fortran]从二维非结构化网格中插入数据以形成笛卡尔网格

vim - ctags 和 Fortran 的接口(interface)

python - 这段 Fortran 77 代码的意图是什么?

python - Fortran 和 Python 中的精度和多项式评估

c - 将二维数组从 Fortran 传递到 C

c - 将多维 C 数组传递给 FORTRAN 函数进行修改

子例程中的数组,Fortran 77

ios - 是否可以为 iPad 编译 Fortran 代码?