【发布时间】:2012-07-17 23:44:13
【问题描述】:
编译器: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++ 中使用前向类声明一样?
【问题讨论】:
-
ifort 13.0.0.041 编译你的模块没有错误,只对没有定义返回值的函数发出警告。
-
太好了,谢谢马克,我会更新的。
标签: oop constructor module fortran circular-dependency