Fortran中函数重载时参数排名不匹配

4
我有一个派生自Vector的类型。
   Type :: Vector

     Real (Real32),  Allocatable :: r32(:)
     Real (Real64),  Allocatable :: r64(:)
     Real (Real128), Allocatable :: r128(:) 

   Contains

     Procedure :: set => vector_set,    & 
                         vector_tutvc,  &
                         vector_vctvc

如果我像(a)这样调用子程序,一切都能正常工作,但是当使用(b)时,我会得到以下错误信息:
错误:参数'u'的级别不匹配(标量和秩-1)。
(a) Call vcr % vector_tutvc (r)

(b) Call vcr % set (r)

以下是更多详细信息:

以下是更多详细信息

 Subroutine vector_set (t, u, v, w)
   Class (Vector), Intent(InOut) :: t
   Class (*), Intent (In) :: u
   Class (*), Intent (In), Optional :: v, w

 Subroutine vector_tutvc (u, tu)
   Class (Vector), Intent(InOut) :: u
   Class (*), Intent (In) :: tu(:)

这是一个测试程序的代码。
 Type (Vector) :: vcr
 Real (Real32), Allocatable :: r(:)

 r = [                                                    &
   1.0000000, 0.9999965, 0.9999931, 0.9999896, 0.9999862,  &
   0.9999829, 0.9999796, 0.9999763, 0.9999731, 0.9999699,  &
   0.9999668, 0.9999637, 0.9999607                         &
 ]

 Call vcr % set (r)
2个回答

4
在类型限定过程声明中
procedure :: binding => procedure

类型限定过程procedure具有绑定名binding

从你提到的重载和缩进选择来看,似乎你期望这个语句:

Procedure :: set => vector_set,    & 
                    vector_tutvc,  &
                    vector_vctvc

使绑定名称set成为通用的,指代这些过程中的每一个。实际上,上述语句与以下语句相同:

Procedure :: set          => vector_set,    & 
             vector_tutvc => vector_tutvc,  &
             vector_vctvc => vector_vctvc

为了实现重载,您需要使用泛型绑定,例如:
Procedure :: vector_set, vector_tutvc,vector_vctvc
Generic :: set => vector_set, vector_tutvc, vector_vctvc

1
    module type_Vector

    use, intrinsic :: iso_fortran_env, only: &
        sp => REAL32, &
        dp => REAL64, &
        qp => REAL128

    ! Explicit typing only
    implicit none

    ! Everything is private unless stated otherwise
    private
    public :: Vector

    ! Declare derived data type
    type, public :: Vector
        real (sp), allocatable :: r32(:)
        real (dp), allocatable :: r64(:)
        real (qp), allocatable :: r128(:)
    contains
        procedure, private :: vector_set
        procedure, private :: vector_tutvc
        generic,   public  :: set => &
            vector_set, &
            vector_tutvc
    end type Vector

contains

    subroutine vector_set(this, u, v, w)
        class (Vector),      intent(in out) :: this
        class (*),           intent (in)    :: u
        class (*), optional, intent (in)    :: v
        class (*), optional, intent (in)    :: w
    end subroutine vector_set

    subroutine vector_tutvc(this, tu)
        class (Vector), intent (in out) :: this
        class (*),      intent (in)     :: tu(:)
    end subroutine vector_tutvc

end module type_Vector

program main

    use, intrinsic :: iso_fortran_env, only: &
        sp => REAL32, &
        stdout => OUTPUT_UNIT, &
        compiler_version, &
        compiler_options

    use type_Vector, only: &
        Vector

    ! Explicit typing only
    implicit none

    type (Vector)          :: vcr
    real (sp), allocatable :: r(:)

    r = [ &
        1.0000000_sp, 0.9999965_sp, 0.9999931_sp, 0.9999896_sp, 0.9999862_sp, &
        0.9999829_sp, 0.9999796_sp, 0.9999763_sp, 0.9999731_sp, 0.9999699_sp, &
        0.9999668_sp, 0.9999637_sp, 0.9999607_sp &
        ]

    call vcr%set(r)

    write( stdout, '(/4A/)' ) 'This file was compiled by ', &
        compiler_version(), ' using the options ', &
        compiler_options()

end program main

set现在是一个泛型类型限定的过程。

This file was compiled by GCC version 5.3.1 20160528 using the options -mtune=generic -march=x86-64 -O3 -std=f2008ts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接