Fortran Coder

查看: 2356|回复: 5
打印 上一主题 下一主题

[派生类型] Error: The AUTOMATIC object 'tempparticles' must not have the SAVE attribute...

[复制链接]

250

帖子

0

主题

0

精华

版主

World Analyser

F 币
682 元
贡献
493 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
楼主
发表于 2022-5-30 22:46:09 | 显示全部楼层
代码用复制粘贴的多好,截图不是个好方法。比如我想帮你测,但懒得把代码敲一遍。

250

帖子

0

主题

0

精华

版主

World Analyser

F 币
682 元
贡献
493 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
沙发
发表于 2022-5-31 09:08:57 | 显示全部楼层
本帖最后由 kyra 于 2022-5-31 09:20 编辑

你这代码我在 intel fortran 上也编译不过呀。

派生类型参数化,本来大家支持得就不好。尽量避免使用。
我想到的修改方案:
第一个:
  integer::x=1 改为: integer , parameter :: x = 1  取消派生类型参数化后的 allocatable ,如果你要 allocatable,就没有参数化的意义了。

[Fortran] 纯文本查看 复制代码
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] 纯文本查看 复制代码
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] 纯文本查看 复制代码
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


250

帖子

0

主题

0

精华

版主

World Analyser

F 币
682 元
贡献
493 点

新人勋章美女勋章元老勋章热心勋章规矩勋章管理勋章

QQ
板凳
发表于 2022-5-31 23:25:43 | 显示全部楼层
CopyTempParticles=TempParticles
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-14 19:09

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表