本帖最后由 kyra 于 2022-5-31 09:20 编辑  
 
你这代码我在 intel fortran 上也编译不过呀。 
 
派生类型参数化,本来大家支持得就不好。尽量避免使用。 
我想到的修改方案: 
第一个: 
  integer::x=1 改为: integer , parameter :: x = 1  取消派生类型参数化后的 allocatable ,如果你要 allocatable,就没有参数化的意义了。 
 
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode module ModParticle
  implicit none
  integer,parameter::x=4
  type ParticleType(n)
    integer(4), len :: n
    real(4):: MassGas(n)
  end type
contains ! Routines of this module
  Subroutine CopyParticle(this, ID)
    type(ParticleType(*)) :: this
    integer(4), intent(in) ::  ID
    this%MassGas = 0.1
  End Subroutine CopyParticle
  
end module
PROGRAM Main
  use ModParticle
  implicit none
  integer::i
  type(ParticleType(x))::TempParticles
  do i = 1, 1
    call CopyParticle(TempParticles,i)
  end do
  write(*,*) TempParticles%MassGas
END PROGRAM Main 
 
如果不取消 parameter,在 gfortran 9.x 上无法实现。intel fortran 可以这样 
 
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode module ModParticle
  implicit none
  integer::x=4
  type ParticleType(n)
    integer(4), len :: n
    real(4) :: MassGas(n)
  contains
    Procedure :: CopyParticle
  end type
contains ! Routines of this module
  Subroutine CopyParticle( this , ID )
    integer(4), intent(in) ::  ID
    class(ParticleType(*)) :: this    
    this%MassGas = 0.1
  End Subroutine CopyParticle
end module
PROGRAM Main
  use ModParticle
  implicit none
  integer::i
  type(ParticleType(:)),allocatable::TempParticles
  allocate(ParticleType(x)::TempParticles)
  do i = 1, TempParticles%n
    call TempParticles%CopyParticle(i)
  end do
  write(*,*) TempParticles%MassGas
  deallocate(TempParticles)
END PROGRAM Main 
 
第三,放弃参数化 
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode module ModParticle
  implicit none
  integer::x=4
  type ParticleType
    real(4) , allocatable :: MassGas(:)
  contains
    Procedure :: CopyParticle
  end type
contains ! Routines of this module
  Subroutine CopyParticle( this , ID )
    integer(4), intent(in) ::  ID
    class(ParticleType) :: this    
    this%MassGas = 0.1
  End Subroutine CopyParticle
end module
PROGRAM Main
  use ModParticle
  implicit none
  integer::i
  type(ParticleType)::TempParticles
  allocate(TempParticles%MassGas(x))
  do i = 1, size(TempParticles%MassGas)
    call TempParticles%CopyParticle(i)
  end do
  write(*,*) TempParticles%MassGas
  deallocate(TempParticles%MassGas)
END PROGRAM Main 
 
 |