Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Module 在现代Fortran中,使用模块内的子例程加载派生类型_Module_Fortran_Gfortran_Subroutine - Fatal编程技术网

Module 在现代Fortran中,使用模块内的子例程加载派生类型

Module 在现代Fortran中,使用模块内的子例程加载派生类型,module,fortran,gfortran,subroutine,Module,Fortran,Gfortran,Subroutine,目标:使用子例程load\u 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 a

目标:使用子例程
load\u 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
对以下网页进行了研究,但未获得成功:

,

,


您的模块没有名为
load\u things
的子例程-它有名为
load\u things\u sub
的子例程。选择正确的名称变体,然后适当地更正另一条语句中的名称

在最初提交该答案之后,OP向模块添加了一条私有语句,使得使用该模块的作用域中无法访问
load\u things\u sub
。在没有其他信息的情况下,应删除该私人声明

通过绑定引用诸如
load\u thing\u sub
之类的过程没有什么意义。只需将其作为正常程序引用即可

请注意,列出的许多参考资料都是针对Fortran 90的。绑定是在Fortran 2003中引入的。

因为
load\u things
是派生类型
反射的绑定名称。正如答案所说,它不是子程序的名称

然后,IanH说,您可以将代码更改为

call load_things_sub ( nThings, reflections )
但您还需要使其成为模块的公共实体。您可能希望使用类型绑定方式,以便它可以保持私有(可以访问类型本身的绑定):

这就引出了另一个观点:你不能做上述事情

您正在为绑定使用
nopass
,因为该类型的伪参数是可分配数组:您不能使用
pass
。但是,在主程序中,伪参数
reflections
是不可分配的标量:存在不匹配,因此
调用load\u things\u sub(nThings,reflections)
无效

进一步

type ( su2 ), allocatable :: reflections(:)
call reflections%load_things ( nThings, reflections )
它本身是无效的。首先,对于
调用反射%…
反射必须被分配。其次,对于
reflections
,绑定不允许使用数组
nopass


这会让你怎么办?嗯,您必须修复
反射的可分配性
,但最简单的方法可能是将
load\u things\u sub
公开,并坚持第一条路径,摆脱类型绑定过程。

过程,不通过,public::load\u things=>load\u things\u sub
应该提供公共名称
load\u things
@dantopa,这是一个绑定名称,您必须将其作为类型绑定过程调用
实例%load\u things
,否则它是无效的。@Vladimir:我的排列已经尝试过了。鉴于此代码示例,应如何具体修改
调用
?如果没有其他信息,您当前的方法(您对过程具有约束力)就没有意义。如果您只是试图调用一个过程,那么只需调用该过程即可。如果必须通过绑定引用该过程,那么您需要向我们解释为什么会这样;调用其他对象%load\u things(n个元素,反射)
但如果OP没有其他信息,那么复杂性似乎毫无意义。鉴于这些参考资料,我怀疑他们只是想直接调用程序。同意所有观点,@IanH。我本打算加强我的最后一段,但我看到你们的答案被编辑成了一个更加简洁的“不要麻烦”的摘要。另一种选择是创建一个容器类型,但对于所呈现的场景来说,这同样过于复杂。@francescalus:您的“最容易做的事情”非常简单。疲劳通过排列诱导编程;感谢您的解决方案和敏锐的分析。
type ( su2 ), allocatable :: reflections(:)
call reflections%load_things ( nThings, reflections )