在现代Fortran中的模块内使用子例程加载派生类型 [英] Loading derived types with a subroutine within a module in modern Fortran
问题描述
目标:使用子例程load_things
加载类型为su2
的结构库.
运行gfortran simple.f90
产生
Undefined symbols for architecture x86_64:
"_load_things_", referenced from:
_MAIN__ in cc7DuxGQ.o
(maybe you meant: ___myclass_MOD_load_things_sub)
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
主程序如下:
program simple
use myClass
implicit none
integer :: nThings
type ( su2 ) :: reflections
call load_things ( nThings, reflections )
end program simple
模块定义为:
module myClass
implicit none
type :: su2
integer :: matrix_obj ( 1 : 2, 1 : 2 )
contains
private
procedure, nopass, public :: load_things => load_things_sub
end type su2
private load_things_sub
contains
subroutine load_things_sub ( nThings, U )
integer, intent ( out ) :: nThings
type ( su2 ), intent ( out ), allocatable :: U ( : )
nThings = 2
allocate ( U ( nThings ) )
U ( 1 ) % matrix_obj = reshape ( [ 0, 1, 1, 0 ], [ 2, 2 ] )
U ( 2 ) % matrix_obj = reshape ( [ 0, 1, 1, 0 ], [ 2, 2 ] )
end subroutine load_things_sub
end module myClass
研究了以下网页,但没有成功:正确使用fortran ,
中的模块,子例程和函数Fortran 90-将值从主子例程传输到函数和其他子例程,
Fortran:从另一个模块中的过程中调用一个模块中的函数,
为 The main program follows: The module definition is: The following web pages were studied without success: Correct use of modules, subroutines and functions in fortran, Fortran 90 - to transmit values from main subroutine to the functions and other subroutines, Fortran: Calling a function in a module from a procedure in another module, Fortran 90 How to call a function in a subroutine in a module? As Vladimir F comments As, then, IanH says, you could change your code to but you'll also need to make that a public entity of the module. You're probably wanting to use the type-bound way so that it can remain private (the binding of the type itself being accessible): Which brings up another set of points: you can't do the above. You're using Further itself isn't valid. First, for Where does that leave you? Well, you'll have to fix the allocatability of 这篇关于在现代Fortran中的模块内使用子例程加载派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!program simple
use myClass
implicit none
integer :: nThings
type ( su2 ) :: reflections
call load_things ( nThings, reflections )
end program simple
module myClass
implicit none
type :: su2
integer :: matrix_obj ( 1 : 2, 1 : 2 )
contains
private
procedure, nopass, public :: load_things => load_things_sub
end type su2
private load_things_sub
contains
subroutine load_things_sub ( nThings, U )
integer, intent ( out ) :: nThings
type ( su2 ), intent ( out ), allocatable :: U ( : )
nThings = 2
allocate ( U ( nThings ) )
U ( 1 ) % matrix_obj = reshape ( [ 0, 1, 1, 0 ], [ 2, 2 ] )
U ( 2 ) % matrix_obj = reshape ( [ 0, 1, 1, 0 ], [ 2, 2 ] )
end subroutine load_things_sub
end module myClass
load_things
is a binding name of the derived type reflections
. It isn't, as the answer says, the name of a subroutine.call load_things_sub ( nThings, reflections )
call reflections%load_things(nThings, reflections)
nopass
for the binding as the dummy argument of the type is an allocatable array: you can't use pass
. However, in your main program the dummy argument reflections
is a non-allocatable scalar: there's a mismatch there so call load_things_sub(nThings, reflections)
isn't valid.type ( su2 ), allocatable :: reflections(:)
call reflections%load_things ( nThings, reflections )
call reflections%...
reflections
must be allocated. Second, with reflections
an array nopass
isn't allowed for the binding.reflections
, but the easiest thing to do is possibly to just go along with making load_things_sub
public and stick with the first path, getting rid of the type-bound procedure.