在 Fortran 中重载时参数中的排名不匹配
Rank mismatch in argument when Overloading in fortran
我有一个 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)
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
在类型绑定过程声明中
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
我有一个 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)
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
在类型绑定过程声明中
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