派生类型声明中的错误:此上下文中 (1) 处的变量必须是常量
Error in Derived type declaration: Variable at (1) in this context must be constant
我在这样的模块中声明了一个派生类型:
MODULE dmotifs
TYPE :: PRM
INTEGER, PRIVATE :: nsp=4,nrx=8,maxprx=4
REAL, PRIVATE :: cref=1e-6,tref=1
REAL, DIMENSION(nrx,maxprx) :: k
REAL :: input
END TYPE PRM
CONTAINS
SUBROUTINE unreg(y,param,r,s)
TYPE(PRM), INTENT(IN) :: param
REAL, DIMENSION(param%nsp), INTENT(IN) :: y
INTEGER, DIMENSION(param%nsp,param%nrx), INTENT(OUT) :: s=0
REAL, DIMENSION(param%nrx,1), INTENT(OUT) :: r=0
REAL :: mOut, mCtrl, pOut, pCtrl
mOut=y(ind_mOut)
mCtrl=y(ind_mCtrl)
pOut=y(ind_pOut)
pCtrl=y(ind_pCtrl)
! <some operations on "r" and "s">
RETURN
END SUBROUTINE unreg
END MODULE dmotifs
编译时出现此错误:
Error: Variable 'nrx' at (1) in this context must be constant
"must be a constant"是什么意思;它在编译期间应该是不可变的,即像参数一样吗?
但还有一个问题,我不能在派生类型中声明参数。如何处理这个错误?将这些对象移出派生类型并使它们成为参数是唯一的选择吗?
最重要的是,我想了解为什么会发生这种情况。
我正在使用 gfortran 进行编译:gfortran -Wall -c "dmotifs.f90"
是的。在非参数化派生类型中声明显式数组需要常量表达式。你可以
- make
k
allocatable,dimension(:,:)
(和(取消)分配),或
- 使
nrx
和 maxprx
global/module 常量(或立即替换它们)。
如果你的compiler supports it, you can use parameterized derived types:
type :: PRM(nrx,maxprx) ! parameterized derived type definition
integer, len :: nrx
integer, len :: maxprx
real :: k(nrx,maxprx)
! ...
end type PRM
(从 here 中提取和调整。)
我在这样的模块中声明了一个派生类型:
MODULE dmotifs
TYPE :: PRM
INTEGER, PRIVATE :: nsp=4,nrx=8,maxprx=4
REAL, PRIVATE :: cref=1e-6,tref=1
REAL, DIMENSION(nrx,maxprx) :: k
REAL :: input
END TYPE PRM
CONTAINS
SUBROUTINE unreg(y,param,r,s)
TYPE(PRM), INTENT(IN) :: param
REAL, DIMENSION(param%nsp), INTENT(IN) :: y
INTEGER, DIMENSION(param%nsp,param%nrx), INTENT(OUT) :: s=0
REAL, DIMENSION(param%nrx,1), INTENT(OUT) :: r=0
REAL :: mOut, mCtrl, pOut, pCtrl
mOut=y(ind_mOut)
mCtrl=y(ind_mCtrl)
pOut=y(ind_pOut)
pCtrl=y(ind_pCtrl)
! <some operations on "r" and "s">
RETURN
END SUBROUTINE unreg
END MODULE dmotifs
编译时出现此错误:
Error: Variable 'nrx' at (1) in this context must be constant
"must be a constant"是什么意思;它在编译期间应该是不可变的,即像参数一样吗?
但还有一个问题,我不能在派生类型中声明参数。如何处理这个错误?将这些对象移出派生类型并使它们成为参数是唯一的选择吗?
最重要的是,我想了解为什么会发生这种情况。
我正在使用 gfortran 进行编译:gfortran -Wall -c "dmotifs.f90"
是的。在非参数化派生类型中声明显式数组需要常量表达式。你可以
- make
k
allocatable,dimension(:,:)
(和(取消)分配),或 - 使
nrx
和maxprx
global/module 常量(或立即替换它们)。
如果你的compiler supports it, you can use parameterized derived types:
type :: PRM(nrx,maxprx) ! parameterized derived type definition
integer, len :: nrx
integer, len :: maxprx
real :: k(nrx,maxprx)
! ...
end type PRM
(从 here 中提取和调整。)