编译器:ifort 版本 12.1.5
我正在编写一些 Fortran 代码,我想利用一些 F2003 OOP 功能,但我遇到了一些绊脚石。简化示例,我希望有两个派生类型 A 和 B,每个类型都有一个指向另一个实例的指针。在 Fortran 中,明确禁止模块之间的循环依赖,因此这两种类型必须驻留在同一个模块中。这编译:
module testModule
implicit none
type A
type(B),pointer :: b1
end type A
type B
type(A),pointer :: a1
end type B
contains
[some possibly type-bound procedures]
end module
现在,我想为这些类型实现一些构造函数,并尝试以下代码:
module testModule
implicit none
type A
type(B),pointer :: b1
end type A
interface A
module procedure A_ctor
end interface
type B
type(A),pointer :: a1
end type B
interface B
module procedure B_ctor
end interface
contains
function A_ctor()
type(A),target :: A_ctor
end function
function B_ctor()
type(B),target :: B_ctor
end function
end module
现在,这不编译,抛出一个错误
这不是派生类型名称。[乙]
在上面的第 5 行。为什么添加接口会报错?如何在 Fortran 中处理派生类型中的循环依赖关系,就像在 C++ 中使用前向类声明一样?