在fortran中重载时参数中的排名不匹配

时间:2016-06-18 00:59:18

标签: fortran overloading

我有一个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)(标量和等级-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 个答案:

答案 0 :(得分: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 :(得分: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现在是generic类型绑定过程。

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