我們如何在類中選擇程序?編譯錯誤是:在Fortran中的類中選擇程序
$ gfortran -Wall -Wextra -Wconversion -Og -pedantic -fcheck=bounds -fbacktrace -g -fmax-errors=5 driver.f08
driver.f08:60:39:
call sub (me, myMeasurements)
1
Error: More actual than formal arguments in procedure call at (1)
發生在subroutine local_selector_sub
內module mIntermediates
(第28行)(行48)。參數sub
將爲compute_intermediates_dot
或compute_intermediates_sum
。
MWE代碼driver.f08
嘗試選擇計算路徑。撥打兩個選項compute_intermediates_sum
和compute_intermediates_dot
都可以工作(第93,94行)。
問題是調用local_selector
(第95行),它包含目標計算路徑的名稱。我們的目標是有不同的表現,如
call myIntermediates % local_selector(compute_intermediates_sum, myMeasurements)
或
call myIntermediates % local_selector(compute_intermediates_dot, myMeasurements)
MWE:
module mMeasurements ! * * *
implicit none
integer, parameter :: m = 2
type :: measurements
real, dimension (1 : m) :: x = 0.0, y = 0.0, ones = 1.0, residuals = 0.0
contains
private
procedure, public :: load_data
end type measurements
contains
subroutine load_data (me)
class (measurements), target :: me
me % x (1) = 1.0 ! load data
me % x (2) = 2.0
me % y (1) = 15.6
me % y (2) = 17.5
end subroutine load_data
end module mMeasurements
module mIntermediatesDefinitions ! * * *
use mMeasurements
implicit none
type :: intermediates
real :: sxy = 0.0
contains
private
procedure, public :: compute_intermediates_dot => compute_intermediates_dot_sub
procedure, public :: compute_intermediates_sum => compute_intermediates_sum_sub
procedure, public :: local_selector => local_selector_sub
end type intermediates
private :: local_selector_sub
private :: compute_intermediates_dot_sub
private :: compute_intermediates_sum_sub
contains
subroutine local_selector_sub (me, sub, myMeasurements) ! problematic routine
class (intermediates), target :: me
type (measurements), intent (in) :: myMeasurements
interface mySub
subroutine sub
use mMeasurements
import intermediates
end subroutine sub
end interface mySub
call sub (me, myMeasurements)
end subroutine local_selector_sub
subroutine compute_intermediates_dot_sub (me, myMeasurements)
class (intermediates), target :: me
type (measurements), intent (in) :: myMeasurements
me % sxy = dot_product (myMeasurements % x, myMeasurements % y)
end subroutine compute_intermediates_dot_sub
subroutine compute_intermediates_sum_sub (me, myMeasurements)
class (intermediates), target :: me
type (measurements), intent (in) :: myMeasurements
me % sxy = sum (myMeasurements % x * myMeasurements % y)
end subroutine compute_intermediates_sum_sub
end module mIntermediatesDefinitions
program driver ! # # #
use mMeasurements
use mIntermediatesDefinitions
implicit none
type (measurements) :: myMeasurements
type (intermediates) :: myIntermediates
call myIntermediates % compute_intermediates_dot (myMeasurements)
call myIntermediates % compute_intermediates_sum (myMeasurements)
call myIntermediates % local_selector (compute_intermediates_dot, myMeasurements)
end program driver
版本: GNU Fortran (GCC) 5.1.0