在 Fortran 中使用面向对象的结构时,我尝试将通用函数分配给结构,如下所示:
!>================================================
type:: PropEq
!>------------------------------------------------
real(dp) :: coeffs(4)
contains
procedure, pass :: compute => computePhysicalProperty
!>------------------------------------------------
end type PropEq
!>================================================
通用子程序接口如下:
!!>================================================
interface computePhysicalProperty
!!>------------------------------------------------
module procedure computePhysicalProperty_scalar
module procedure computePhysicalProperty_array
!!>------------------------------------------------
end interface computePhysicalProperty
!!>================================================
还有两个函数:
!>================================================
function computePhysicalProperty_scalar(propEq, T) result(value)
!>------------------------------------------------
real(dp) :: value
class(PropEqAbstract) :: propEq
real(dp), intent(in) :: T
real(dp), allocatable :: a(:)
a = propEq% coeffs
value = a(1) + a(2)*T + a(3)*T*T + a(4)*T*T*T
!>------------------------------------------------
end function computePhysicalProperty_scalar
!>================================================
!>================================================
function computePhysicalProperty_array(propEq, T) result(value)
!>------------------------------------------------
real(dp) :: value
class(PropEqAbstract) :: propEq
real(dp), intent(in), allocatable :: T(:)
real(dp), allocatable :: a(:)
a = propEq% coeffs
value = a(1) + a(2)*T + a(3)*T*T + a(4)*T*T*T
!>------------------------------------------------
end function computePhysicalProperty_array
!>================================================
本质上,我试图重载该过程以允许函数输入标量和数组。
这样做时,我收到以下错误:
error #8182: The name is neither an abstract interface nor a procedure with an explicit interface. [COMPUTEPHYSICALPROPERTY]
此外,我发现以下似乎有效: 带有过程参数的通用类型绑定过程
!>================================================
type:: PropEq
!>------------------------------------------------
real(dp) :: coeffs(4)
contains
procedure, pass :: compute_scalar => computePhysicalProperty_scalar
procedure, pass :: compute_array => computePhysicalProperty_array
generic :: compute => compute_scalar, compute_array
!>------------------------------------------------
end type PropEq
!>================================================
为什么过程需要在类型中显式声明?
有没有办法使用接口将类型简化为以下?
????? :: compute => computePhysicalProperty
谢谢你
编译器:Intel
IDE:Visual Studio Enterprise
Fortran 语言标准不允许您尝试执行的操作。绑定不能指向通用接口,它必须指向过程。一旦您的类型中拥有了绑定(type=bound procedure),您就可以使用
generic :: compute =>
. 从它们创建通用绑定。
标准语法如下:
R748 type-bound-proc-binding
is type-bound-procedure-stmt
or type-bound-generic-stmt
or final-procedure-stmt
从中你感兴趣
R749 type-bound-procedure-stmt
is PROCEDURE [ [ , binding-attr-list ] :: ] type-bound-proc-decl-list
or PROCEDURE (interface-name), binding-attr-list :: binding-name-list
然后
R750 type-bound-proc-decl is binding-name [ => procedure-name ]
有约束
C774 (R750) The procedure-name shall be the name of an accessible module procedure or an external procedure
that has an explicit interface.
这是您所遇到的最终限制。