-1

我正在用派生类型在 Fortran 中编写一些代码并遇到问题,但仍然无法弄清楚出了什么问题...................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ……

make -f vbld.mk
gfortran -c gshapes.f08
gshapes.f08:100.31:

      generic, public :: get => get_ellipse,       &
                               1
Error: Undefined specific binding 'get_ellipse_minmax' as target of GENERIC 'get' at (1)
gshapes.f08:136.31:

      generic, public :: get => get_cylinder,      &
                               1
Error: Undefined specific binding 'get_cylinder_minmax' as target of GENERIC 'get' at (1)
gshapes.f08:139.15:

      procedure :: print => print_cylinder
               1
Error: Dummy argument 'cyld' of 'print' at (1) should be named 'elips' as to match the     
corresponding argument of the overridden procedure
gshapes.f08:135.15:

      procedure :: set => set_cylinder
               1
Error: Dummy argument 'cyld' of 'set' at (1) should be named 'elips' as to match the 
corresponding argument of the overridden procedure
gshapes.f08:74.31:

      generic, public :: get => get_rectangle,      &
                               1
Error: Undefined specific binding 'get_rectangle_minmax' as target of GENERIC 'get' at (1)
gshapes.f08:118.31:

      generic, public :: get => get_prism,      &
                               1

Error: Undefined specific binding 'get_prism_minmax' as target of GENERIC 'get' at (1)
gshapes.f08:121.15:

      procedure :: print => print_prism
               1
Error: Dummy argument 'prsm' of 'print' at (1) should be named 'rect' as to match the 
corresponding argument of the overridden procedure
gshapes.f08:117.15:

      procedure :: set => set_prism
               1
Error: Dummy argument 'prsm' of 'set' at (1) should be named 'rect' as to match the 
corresponding argument of the overridden procedure
make: *** [gshapes.mod] Error 1
4

2 回答 2

4

泛型绑定指定了在代码中引用泛型绑定时可以考虑的特定绑定。然后,这些特定绑定引用特定过程(并且可以覆盖那些特定过程 - 它们可能是扩展中的不同特定过程)。

您的通用绑定引用过程名称,而不是特定绑定名称。正确的做法是:

TYPE parent
CONTAINS
  PROCEDURE :: SpecificBindingA => ProcedureA
  PROCEDURE :: SpecificBindingB => ProcedureB
  GENERIC :: GenericBinding => SpecificBindingA, SpecificBindingB
END TYPE parent

在代码中,如果声明了一个对象TYPE(parent) :: obj,那么对的引用obj%GenericBinding将解析为obj%SpecificBindingAobj%SpecificBindingB取决于引用中实际参数的类型。然后,动态类型obj将确定为特定特定绑定调用的实际过程。

过程 ProcedureA 和 ProcedureB 需要将它们的第一个虚拟参数声明为适当的,以便它可以成为传递的对象(它必须被声明CLASS(parent),具有相同的参数名称等)。parent 扩展中的任何覆盖都需要适当地改变传递参数的类型,并具有任何其他虚拟参数(包括虚拟参数名称)匹配的特征。

或者,也许您只想要特定过程的通用名称。您可以使用接口块来执行此操作。

INTERFACE GenericName
  MODULE PROCEDURE ProcedureA
  MODULE PROCEDURE ProcedureB
END INTERFACE GenericName

在这种情况下,引用GenericName(...)将被解析为ProcedureAProcedureB取决于引用中的参数。在这种情况下,没有基于对象的动态类型的特定过程的动态查找。

于 2013-05-01T22:06:51.277 回答
0
module shape
implicit none

type, public :: GCoord
  real :: x
  real :: y
  real :: z
end type GCoord

type, extends (GCoord) :: GCentr
end type GCentr

type, extends (GCoord) :: GCornr
end type GCornr

type, public :: Rectangle
  type(GCentr) :: cn
  type(GCoord) :: pa
  real :: b
  type(GCornr) :: p1, p2, p3, p4
  contains
    generic, public :: get => getu, getm
end type Rectangle

contains

subroutine getu (rect, val)
  class(Rectangle), intent(in) :: rect
  integer, intent(in) :: val
  write (*,*) 'GETU'
end subroutine getu

subroutine getm (rect, val)
  class(Rectangle), intent(in) :: rect
  complex, intent(in) :: val
  write (*,*) 'GETM'
end subroutine getm

end module shape
于 2013-05-01T20:45:19.507 回答