问题
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
In gfortran (4.4 & 4.5) I get the following error:
Error: The type for function 'somefunction' at (1) is not accessible
I compiled the file as:
gfortran -c ./test.F90
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.
Why is the type not accessible?
回答1:
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
回答2:
The answer to the question why it is not accessible is that the standard committee designed it like that. The interface has a separate scope from the enclosing module, so you have to explicitly import names from it. Obviously(?) you can't use
the module inside itself, so the import
statement is needed.
来源:https://stackoverflow.com/questions/8751195/why-is-the-type-not-accessible