gfortran关联错误的类型绑定过程
当我们编译(gfortran 5.3或7.2)并运行以下代码时,main.f03的第9行以一个从未调用过的子例程结束。有人能解释为什么吗 main.f03:gfortran关联错误的类型绑定过程,fortran,procedure,gfortran,derived-types,Fortran,Procedure,Gfortran,Derived Types,当我们编译(gfortran 5.3或7.2)并运行以下代码时,main.f03的第9行以一个从未调用过的子例程结束。有人能解释为什么吗 main.f03: program main use minimalisticcase implicit none type(DataStructure) :: data_structure type(DataLogger) :: data_logger call data_structure%init()
program main
use minimalisticcase
implicit none
type(DataStructure) :: data_structure
type(DataLogger) :: data_logger
call data_structure%init()
call data_logger%init(data_structure)
end program
最低限度案例1.f03:
module minimalisticcase
implicit none
type, public :: DataStructure
integer :: i
contains
procedure, pass :: init => init_data_structure
procedure, pass :: a => beginning_of_alphabet
end type
type, public :: DataLogger
type(DataStructure), pointer :: data_structure
contains
procedure, pass :: init => init_data_logger
procedure, pass :: do_something => do_something
end type
contains
subroutine init_data_structure(self)
implicit none
class(DataStructure), intent(inout) :: self
write(*,*) 'init_data_structure'
end subroutine
subroutine beginning_of_alphabet(self)
implicit none
class(DataStructure), intent(inout) :: self
write(*,*) 'beginning_of_alphabet'
end subroutine
subroutine init_data_logger(self, data_structure)
implicit none
class(DataLogger), intent(inout) :: self
class(DataStructure), target :: data_structure
write(*,*) 'init_data_logger'
self%data_structure => data_structure
call self%do_something()
end subroutine
subroutine do_something(self)
implicit none
class(DataLogger), intent(inout) :: self
write(*,*) 'do_something'
end subroutine
end module
在“minimalisticcase.f03”的第40行,我们称之为数据记录器的“do_something”。但是,数据结构的“字母表的开始”子例程被执行
显然,可以通过将“minimalisticcase.f03”中的第13行从类型(数据结构)、指针::数据结构
更改为类(数据结构)、指针::数据结构
来解决这个问题
但是为什么呢?这是gfortran中的一个bug。我把它贴在Bugzilla上。该错误现在已在GCC主干上修复 临时解决方法是将指针赋值封装在选择类型中,因此:
select type (data_structure)
type is (DataStructure)
self%data_structure => data_structure
end select
iFort17和gfortran 7.2的一切都如我所期望的那样工作。首先是
init\u data\u structure
,然后是init\u data\u logger
,最后是做点什么
@Matt:这很有趣!我可以在我的windows和linux机器上重现这种行为。你在哪个平台上工作?我在Windows10(64位)上测试了这个,在WSL中使用gfortran。报告欢迎Paul。顺便说一句,我们通常不会在这里签名和使用问候语,因为你的名字会自动放在右边图标旁边的帖子下方。你的解决方法与我在问题中提到的不同。有理由选择其中一个而不是另一个吗?(感谢您提交报告)该漏洞现在已在7-branch上修复并关闭。谢谢你的报告。