2016-06-18 21 views
4

にオーバーロードするとき、私はベクトルの派生型ランクの不一致FORTRAN

Type :: Vector 

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

    Contains 

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

私はしかし、(B)を使用している場合、正しく すべてが働いてもらう(A)のように、私はサブルーチンを呼び出すと、私は取得していますがあります

エラー: '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) 

答えて

4

の過負荷のあなたの言及とインデントのあなたの選択から、あなたは声明

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するためのコードで今genericタイプバインドの手順です。型結合プロシージャ宣言

procedure :: binding => procedure 

バインディング名bindingを有するprocedure型結合手順において

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