带有参数列表的Fortran类型定义 [英] Fortran Type Definition with a parameter list
问题描述
$ 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
;
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, eitherlen
orkind
; - 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屋!