fortran - 如何解决关于使用 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
解决方案
这里的解决方法非常简单,并出现在最近的问题/答案之一中。只需复制整个数组
local%atom = list%atom
但是,当您确实需要访问单个元素时,并不总是可以这样做。如果您的真实用例是这样的,请展示真实用例。
如果内部可能的类型数量有限,您也可以使用select type
类型保护,但通常这也是不可能的。
推荐阅读
- excel - isnumber 公式在数字存在时提取某些值,在文本字符串存在时提取其他值?
- python - 基于特定组(例如类别动作)在python中创建条形图并根据持续时间显示电影的所有名称
- android - 无法使用 vararg 参数调用函数,而不是来自 java 的 vararg 参数
- c# - 在组件上画线
- arrays - NSDictionary Swift 4
- powershell - 如何使用 Powershell 在 OneNote 文件中搜索文本
- git - Git 从提交历史中删除文件
- curl - 卷曲下载档案的一小部分
- kubernetes - 使用限制性出口网络策略访问服务/kubernetes
- javascript - 值滑块的问题,它们相互干扰