带有参数列表的Fortran类型定义 [英] Fortran Type Definition with a parameter list

查看:266
本文介绍了带有参数列表的Fortran类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

 程序main $请注意这段代码,为什么有问题,我该怎么做呢? b $ b类型矩阵(m,n)
整数:: m,n
双精度,维(1:m,1:n)::值
结束类型矩阵

类型(矩阵(2,3)):: B
print *,OK
结束程序

关于这个的另一个问题是:我可以有一个类型定义或模块定义后跟一个参数列表吗?因为我从书中看到了这段代码,不知道为什么我不能编译它。 解决方案

撇开MSB对点的观察定义 matrix ,如果你有一个最新的编译器,你可以定义和声明一个参数化的定义类型,就像这样:

  type matrix(m,n,k)
整数,len :: m,n
整数,kind :: k
real(kind = k),dimension(m,n):: elements
结束类型矩阵

...

类型(矩阵(4,3,selected_real_kind(0.0)):: the_matrix

注意:并非所有广泛使用的Fortran编译器都实现了2008标准的这一功能;

  • 参数 m,n,k 在其声明中有一个特殊属性,可以是 len kind ;

  • 我已经包含kind-type参数来表明它存在,所以不需要在定义参数化派生类型时包含它。


  • Please take a look at this code, why there are problems, how could I do this instead?

    program main
    type matrix(m,n)
        integer::m,n
        double precision,dimension(1:m,1:n)::value
    end type matrix
    
    type(matrix(2,3))::B
    print*,"OK"
    end program
    

    Another question about this is: Can I have a type definition or module definition followed by a parameter list? Cause I saw this code from book , don't know why I cannot compile it.

    解决方案

    Leaving aside MSB's observation about the point of defining matrix, and if you have a bang up-to-date compiler you could define and declare a parameterized defined-type rather like this:

    type matrix(m,n,k)
        integer, len :: m,n
        integer, kind :: k
        real(kind=k), dimension(m,n) :: elements
    end type matrix
    
    ...
    
    type(matrix(4,3,selected_real_kind(0.0)) :: the_matrix
    

    Note:

    • not all the widely-used Fortran compilers yet implement this feature of the 2008 standard;
    • the parameters m,n,k have a special attribute on their declaration, either len or kind;
    • I've included the kind-type parameter just to show that it exists, it's not necessary to include it when you define a parameterised derived type.

    这篇关于带有参数列表的Fortran类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆