我正在尝试定义 interface
d 过程作为 Fortran 中的类型绑定(bind)过程 type
定义,但它似乎不像人们期望的那样工作。考虑以下模块:
module example_module
implicit none
private
interface add_them
module procedure add_them_integer,add_them_real
end interface add_them
type, public :: foo
integer, private :: a=1,b=2
real, private :: c=4.,d=5.
contains
procedure, public :: add => add_them
end type foo
contains
subroutine add_them_integer(self,x)
class(foo), intent(in) :: self
integer, intent(in) :: x
print *,self%a+self%b+x
end subroutine add_them_integer
subroutine add_them_real(self,x)
class(foo), intent(in) :: self
real, intent(in) :: x
print *,self%c+self%d+x
end subroutine add_them_real
end module example_module
以及使用该模块的相应程序:
program example
use example_module
implicit none
type(foo) :: foofoo
call foofoo%add(1)
call foofoo%add(2.)
end program example
我希望它可以编译,结果应该是 4 和 11。但是,gfortran 报告以下错误:
procedure, public :: add => add_them
1
Error: 'add_them' must be a module procedure or an external procedure with an explicit interface at (1)
一种解决方法是使用
generic
类型绑定(bind)过程而不是 interface
d 一,使模块如下:module example_module
implicit none
private
type, public :: foo
integer, private :: a=1,b=2
real, private :: c=4.,d=5.
contains
generic, public :: add => add_them_integer,add_them_real
procedure, private :: add_them_integer,add_them_real
end type foo
contains
subroutine add_them_integer(self,x)
class(foo), intent(in) :: self
integer, intent(in) :: x
print *,self%a+self%b+x
end subroutine add_them_integer
subroutine add_them_real(self,x)
class(foo), intent(in) :: self
real, intent(in) :: x
print *,self%c+self%d+x
end subroutine add_them_real
end module example_module
这按预期工作。但是,我不能使用
generic
程序。以上只是演示问题的简化示例,但在我的实际代码中,'add_them' 不能是 generic
过程,因为“foo”实际上是派生类型,而“add_them”覆盖了父类型中定义的过程; gfortran(至少)不允许 generic
覆盖基本过程的过程。为了绕过这个限制,我想我应该使用 interface
相反,但正如您在上面的示例中看到的那样,虽然 'add_them' 定义正确,但编译器提示“'add_them' 必须是模块过程或具有显式接口(interface)的外部过程”。任何帮助,将不胜感激;提前致谢。
最佳答案
您的第一段代码的 gfortran 错误是正确的。进行通用绑定(bind)的方法是按照您的“按预期工作”部分的代码。
如果父类型具有特定名称的特定绑定(bind),则您不能在扩展中重用该名称,除非覆盖特定绑定(bind)。
如果你想要add
(注意名称 add_them
没有出现在您的第二种情况中)成为扩展中的通用绑定(bind),然后使其成为父级中的通用绑定(bind)。
关于Fortran 中的接口(interface)类型绑定(bind)过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436475/