5

我正在尝试从 fortran 函数返回一个类型。这是代码。

module somemodule
implicit none
! define a simple type
type sometype
   integer :: someint
end type sometype
! define an interface 
interface
   ! define a function that returns the previously defined type
   type(sometype) function somefunction()
   end function somefunction
end interface
contains
end module somemodule

在 gfortran (4.4 & 4.5) 中,我收到以下错误:

错误: (1) 处的函数“somefunction”的类型不可访问

我将文件编译为:

gfortran -c ./test.F90

我试图将类型明确公开,但这没有帮助。我本来打算使用 ac 版本的 somefunction,这就是为什么我把它放在接口部分。

为什么类型不可访问?

4

2 回答 2

7

在函数定义中添加import可以解决这个问题。由于许多人认为语言设计中的错误,定义不会在接口内部继承。“导入”会覆盖它以实现明智的行为。

interface
   ! define a function that returns the previously defined type
   type(sometype) function somefunction()
   import
   end function somefunction
end interface
于 2012-01-06T00:05:23.087 回答
3

为什么它无法访问的问题的答案是标准委员会是这样设计的。该接口具有与封闭模块不同的范围,因此您必须从中显式导入名称。显然(?)你不能use把模块放在里面,所以import需要声明。

于 2012-01-06T00:06:30.823 回答