Fortran OOP循环依赖处理,接口 [英] Fortran OOP circular dependency handling, interfaces

查看:557
本文介绍了Fortran OOP循环依赖处理,接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Compiler:ifort version 12.1.5

Compiler: ifort version 12.1.5

我正在写一些Fortran代码,我想使用一些F2003的OOP功能,击中一些绊脚石。省略示例,我希望有两个派生类型A和B,每个派生类型都有一个指向另一个实例的指针。在Fortran中,模块之间的循环依赖性被明确禁止,因此这两种类型必须驻留在同一个模块中。它编译:

I'm writing some Fortran code and I'd like to make use of some F2003 OOP features, but I'm hitting some stumbling blocks. Paring down the example, I wish to have two derived types A and B, each of which have a pointer to instances of the other. In Fortran, circular dependencies between modules are explicitly disallowed, so these two types would have to reside in the same module. This compiles:

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

现在,我想为这些类型实现一些构造函数,并尝试以下代码:

Now, I want to implement some constructors for these types, and try this code:

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


$ b b

现在,这不会编译,引发错误

Now, this doesn't compile, throwing an error


这不是派生类型名称。 [B]

This is not a derived type name. [B]

。为什么添加接口会抛出错误?如何在Fortran中处理导出类型的循环依赖,因为在C ++中会使用转发类声明?

on line 5 above. Why does adding the interfaces throw an error? How does one handle circular dependencies in derived types in Fortran, as one would use a forward class declaration in C++?

推荐答案

是正确的Fortran 2003.你正在以正确的方式处理循环类型依赖。编译器是错误的。

Your code is correct Fortran 2003. You are handling the circular type dependency in the correct way. The compiler is in error.

我经历过,并看到其他人报告问题与ifort 12.1.5和程序员覆盖的结构构造函数(其中有一个函数的通用接口具有派生类型结果,接口与派生类型具有相同的名称,就像你在这里一样)。这种情况的一种解决方法是将构造函数的通用接口(及后续引用)重命名为与派生类型不同的名称。

I have experienced and seen others report issues with ifort 12.1.5 and programmer overrides of structure constructors (where there is a generic interface to a function that has a derived type result, with the interface having the same name as the derived type - as you have here). A workaround for this situation to rename the generic interface (and subsequent references) for the constructor function to a name different from the derived type.

这篇关于Fortran OOP循环依赖处理,接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆