目标:使用子例程 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
以下网页未经成功研究: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?
2 回答
由于Vladimir F comments
load_things
是派生类型reflections
的绑定名称 . 正如答案所说,它不是子程序的名称 .那么,IanH说,您可以将代码更改为
但你还需要将其作为模块的公共实体 . 您可能希望使用类型绑定方式,以便它可以保持私有(可以访问类型本身的绑定):
这带来了另一套要点:你不能做到以上几点 .
您使用
nopass
进行绑定,因为该类型的伪参数是可分配的数组:您不能使用pass
. 但是,在您的主程序中,伪参数reflections
是一个不可分配的标量:那里存在不匹配,因此call load_things_sub(nThings, reflections)
无效 .进一步
本身无效 . 首先,必须分配
call reflections%...
reflections
. 其次,使用reflections
时,不允许绑定数组nopass
.那你离开了哪里?好吧,你将不得不修复
reflections
的可配置性,但最容易做的事情可能只是让load_things_sub
公开并坚持第一条路径,摆脱类型绑定程序 .您的模块没有名为
load_things
的子程序 - 它有一个名为load_things_sub
的子程序 . 选择正确的名称变体,然后相应地更正另一个语句中的名称 .在最初提交此答案之后,OP向模块添加了一个私有语句,该语句使得使用该模块的范围内的
load_things_sub
不可访问 . 在没有其他信息的情况下,应删除私人声明 .通过绑定引用诸如
load_thing_sub
之类的过程是没有意义的 . 只需将其作为正常程序引用即可 .请注意,许多列出的引用都是针对Fortran 90.绑定是在Fortran 2003中引入的 .