在How to pass subroutine names as arguments in Fortran?中,我們學習瞭如何在Fortran中傳遞子例程名稱作爲參數。我們如何在類結構中做到這一點?如何在Fortran類中將子例程名稱作爲參數傳遞
隨後的代碼產生使用GNU的Fortran(GCC)5.1.0以下編譯錯誤:
gfortran -Wall -Wextra -Wconversion -Og -pedantic -fcheck=bounds -fmax-errors=5 class_pass.f08
myClass.f08:44:30:
class (test), target :: me
1
Error: Derived type ‘test’ at (1) is being used before it is defined
myClass.f08:9:21:
procedure, public :: action => action_sub
1
Error: Non-polymorphic passed-object dummy argument of ‘action_sub’ at (1)
myClass.f08:40:36:
class (test), target :: me
1
Error: CLASS variable ‘me’ at (1) must be dummy, allocatable or pointer
(null):0: confused by earlier errors, bailing out
主程序如下。它包括用作診斷的例程check
。
include 'myClass.f08'
program class_pass
use myClass
implicit none
type (test) :: myTest
call myTest % check()
call myTest % action (square_sub)
end program class_pass
模塊:
module myClass
implicit none
type :: test
real :: x, y
contains
private
procedure, public :: action => action_sub
procedure, public :: square => square_sub
procedure, public :: double => double_sub
procedure, public :: check => check_sub
end type test
private :: action_sub
private :: square_sub
private :: double_sub
private :: check_sub
contains
subroutine square_sub (me)
class (test), target :: me
me % y = me % x ** 2
end subroutine square_sub
subroutine double_sub (me)
class (test), target :: me
me % y = me % x * 2
end subroutine double_sub
subroutine check_sub (me)
class (test), target :: me
me % x = 5.0
call double_sub (me)
print *, 'x = ', me % x, ', y = ', me % y
end subroutine check_sub
subroutine action_sub (sub)
class (test), target :: me
interface mySub
subroutine sub (me)
class (test), target :: me
end subroutine sub
end interface mySub
call sub (me)
print *, 'x = ', me % x, ', y = ', me % y
end subroutine action_sub
end module myClass
非常感謝@Vladimir F到原來的解決方案和提示。
接口塊是一個獨特的作用域單元,所以要'import'。 – francescalus
可能的重複[如何聲明過程參數的接口部分,進而引用相同模塊的用戶派生類型?](http://stackoverflow.com/questions/8549415/how-to-declare -the-interface-section-for-a-procedure-argument-which-in-ref) – francescalus
@francescalus一個非常有用的條目。謝謝。 – dantopa