使用派生类型时Fortran错误

时间:2013-05-01 00:47:04

标签: fortran extends

我在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

2 个答案:

答案 0 :(得分:3)

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

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

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),具有相同的参数名称等)。父对象的扩展中的任何覆盖都需要适当地改变传递的参数的类型,并且具有任何其他伪参数(包括伪参数名称)匹配的特征。

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

INTERFACE GenericName
  MODULE PROCEDURE ProcedureA
  MODULE PROCEDURE ProcedureB
END INTERFACE GenericName

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

答案 1 :(得分: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