派生类型声明中的错误:在这种情况下,(1)处的变量必须为常数 [英] Error in Derived type declaration: Variable at (1) in this context must be constant

查看:235
本文介绍了派生类型声明中的错误:在这种情况下,(1)处的变量必须为常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的模块中声明了一个派生类型:

I have a derived type declared in a module like this:

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

必须是常量的含义是什么;它在编译期间是否应该不变,即像PARAMETER一样?

What is the meaning of "must be a constant"; should it be immutable during compilation i.e. like a PARAMETER?

但是还有另一个问题,我不能在派生类型中声明参数。如何处理这个错误?

But there is another issue, I cannot declare PARAMETERS within derived types. How to deal with this error? Would moving these objects out of the derived type and making them PARAMETERS, be the only option?

最重要的是,我希望了解为什么会发生这种情况。

Most importantly I wish to understand why does this happen.

我正在使用gfortran进行编译: gfortran -Wall -c dmotifs.f90

I was compiling using gfortran: gfortran -Wall -c "dmotifs.f90"

推荐答案

是。声明非参数派生类型的显式数组需要一个常量表达式。您可以

Yes. Declaring an explicit-shape array in a non-parameterized derived type requires a constant expression. You could either


  • 使 k 可分配的尺寸( :,:)(和(取消分配)),或

  • 制作 nrx maxprx 全局/模块常量(或立即替换它们)。

  • make k allocatable,dimension(:,:) (and (de-)allocation), or
  • make nrx and maxprx global/module constants (or replace them right away).

如果您的编译器支持,您可以使用 参数化派生类型

If your 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

(取自此处。)

这篇关于派生类型声明中的错误:在这种情况下,(1)处的变量必须为常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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