在现代Fortran中的模块内使用子例程加载派生类型 [英] Loading derived types with a subroutine within a module in modern Fortran

查看:134
本文介绍了在现代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:从另一个模块中的过程中调用一个模块中的函数

Fortran 90如何在模块的子例程中调用函数?

解决方案

The main program follows:

program simple
  use myClass
  implicit none
  integer :: nThings
  type ( su2 ) :: reflections
      call load_things ( nThings, reflections )
end program simple

The module definition is:

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

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 load_things is a binding name of the derived type reflections. It isn't, as the answer says, the name of a subroutine.

As, then, IanH says, you could change your code to

call load_things_sub ( nThings, reflections )

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):

call reflections%load_things(nThings, reflections)

Which brings up another set of points: you can't do the above.

You're using 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.

Further

type ( su2 ), allocatable :: reflections(:)
call reflections%load_things ( nThings, reflections )

itself isn't valid. First, for call reflections%... reflections must be allocated. Second, with reflections an array nopass isn't allowed for the binding.

Where does that leave you? Well, you'll have to fix the allocatability of 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.

这篇关于在现代Fortran中的模块内使用子例程加载派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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