为什么类型不可访问? [英] Why is the type not accessible?

查看:162
本文介绍了为什么类型不可访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to return a type from a fortran function. This is the code.

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)中出现以下错误:

In gfortran (4.4 & 4.5) I get the following error:


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

Error: The type for function 'somefunction' at (1) is not accessible

我将这个文件编译为:

gfortran -c ./test.F90

我试图让类型显式公开,但是没有帮助。我打算使用某种功能的c版本,这就是为什么我把它放在界面部分。

I tried to make the type explicitly public but that didn't help. I was planning to use a c version of the somefunction, that is why I put it in the interface section.

为什么类型不可访问?

推荐答案

> import 在函数定义内修复了这个问题。由于许多人认为语言设计中存在错误,因此定义不会在界面内继承。 导入将覆盖此项以实现合理的行为。

Adding import inside the function definition fixes this. Due to what many consider a mistake in the design of the language, definitions aren't inherited inside of an interface. The "import" overrides this to achieve the sensible behavior.

interface
   ! define a function that returns the previously defined type
   type(sometype) function somefunction()
   import
   end function somefunction
end interface

这篇关于为什么类型不可访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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