首页 > 解决方案 > 如何解决关于使用 Gfortran 将两个多态组件之间的内在赋值为类型变量的问题?

问题描述

Gfortran 8.1 和 9.1 给我一个关于两个多态组件之间的内在赋值到类型变量的错误。我使用英特尔编译器没有任何问题,但在 gfortran 中没有。我在问是否有人知道任何解决方法。这是一个您可以尝试编译的示例。

Program Check
implicit none

!> Type definitions
Type :: Atm_Type
End Type Atm_Type

Type, extends (Atm_type) :: Atm_Std_Type
End Type Atm_Std_Type

Type, extends (Atm_std_type) :: Atm_Ref_Type
End Type Atm_Ref_Type

Type :: AtList_Type
   integer                                    :: Natoms
   class(Atm_Type), dimension(:), allocatable :: Atom
end Type AtList_Type

!> Variables 
type(AtList_Type) :: list

call sub(list)

Contains

Subroutine Sub(List)
   !---- Argument ----!
   type (AtList_Type), intent(in out) :: List

   !---- Local Variables ----!
   integer            :: i
   type (AtList_Type), allocatable :: local

   if (List%natoms <= 0 ) return
   allocate(local%atom(List%natoms))

   do i=1, List%natoms
      local%atom(i)=list%atom(i)
   end do   

End Subroutine Sub

End Program Check

标签: fortrangfortran

解决方案


这里的解决方法非常简单,并出现在最近的问题/答案之一中。只需复制整个数组

 local%atom = list%atom

但是,当您确实需要访问单个元素时,并不总是可以这样做。如果您的真实用例是这样的,请展示真实用例。

如果内部可能的类型数量有限,您也可以使用select type类型保护,但通常这也是不可能的。


推荐阅读