Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 如何为扩展原始类型的每个类型创建一个可以具有不同输入参数的过程_Fortran_Gfortran - Fatal编程技术网

Fortran 如何为扩展原始类型的每个类型创建一个可以具有不同输入参数的过程

Fortran 如何为扩展原始类型的每个类型创建一个可以具有不同输入参数的过程,fortran,gfortran,Fortran,Gfortran,我有一个派生类型: module type_1_mod implicit none public :: type1 type :: type1 private integer :: a, b contains procedure, public :: mandatoryProcedureForAllExtends end type type1 contains subroutine mandatoryProcedureForAllExten

我有一个派生类型:

module type_1_mod
  implicit none

  public :: type1

  type :: type1
    private
    integer :: a, b
  contains
    procedure, public :: mandatoryProcedureForAllExtends
  end type type1

contains

  subroutine mandatoryProcedureForAllExtends(this, varInt, varReal, varLogic)
    class(type1) :: this
    integer :: varInt
    real :: varReal
    logical :: varLogic

    *** something ***
  end subroutine mandatoryProcedureForAllExtends

end module type_1_mod
我想声明其他派生类型,这些派生类型将扩展我在上面编写的这个派生类型,并且所有派生类型都将具有过程
mandatoryprocedureollextends
,但我希望此过程没有强制结构-这意味着用户可以用另一个具有不同输入参数的派生类型重写该过程,例如:

module type_2_mod
  use type_1_mod
  implicit none

  public :: type2

  type, extends(type1) :: type2
    private
  contains
    procedure, public :: mandatoryProcedureForAllExtends
  end type type2

contains

  subroutine mandatoryProcedureForAllExtends(this, varReal, varReal2, varReal3, varReal4, varLogic)
    class(type2) :: this
    real :: varReal, varReal2, varReal3, varReal4,
    logical :: varLogic

    *** something ***
  end subroutine mandatoryProcedureForAllExtends

end module type_2_mod
因此,扩展type1的类型有一个同名的过程,但被重新定义为具有不同数量和类型的变量

我曾尝试将type1作为摘要编写,并使用延迟过程,但这些过程需要显式地声明一个具有过程结构的接口。我也尝试过使用类似

关于类型1声明

  contains
    procedure, public :: type1VersionOfMandatory
    generic :: mandatoryProcedureForAllExtends => type1VersionOfMandatory

contains

  subroutine type1VersionOfMandatory(this, varInt, varReal, varLogic)
    class(type1) :: this
    integer :: varInt
    real :: varReal
    logical :: varLogic

    *** something ***
  end subroutine type1VersionOfMandatory
  contains
    procedure, public :: type2VersionOfMandatory
    generic :: mandatoryProcedureForAllExtends => type2VersionOfMandatory

contains

  subroutine type2VersionOfMandatory(this, varReal, varReal2, varReal3, varReal4, varLogic)
    class(type2) :: this
    real :: varReal, varReal2, varReal3, varReal4,
    logical :: varLogic

    *** something ***
  end subroutine type2VersionOfMandatory
关于类型2声明

  contains
    procedure, public :: type1VersionOfMandatory
    generic :: mandatoryProcedureForAllExtends => type1VersionOfMandatory

contains

  subroutine type1VersionOfMandatory(this, varInt, varReal, varLogic)
    class(type1) :: this
    integer :: varInt
    real :: varReal
    logical :: varLogic

    *** something ***
  end subroutine type1VersionOfMandatory
  contains
    procedure, public :: type2VersionOfMandatory
    generic :: mandatoryProcedureForAllExtends => type2VersionOfMandatory

contains

  subroutine type2VersionOfMandatory(this, varReal, varReal2, varReal3, varReal4, varLogic)
    class(type2) :: this
    real :: varReal, varReal2, varReal3, varReal4,
    logical :: varLogic

    *** something ***
  end subroutine type2VersionOfMandatory
但我在尝试编译时收到一个错误:
错误:未定义的特定绑定“type1VersionOfMandatory”作为(1)处泛型“MandatoryProcedureRoullExtends”的目标。
错误:未定义的特定绑定“type2VersionOfMandatory”作为(1)处泛型“MandatoryProcedureRoullExtends”的目标。

我还试图通过再次使用相同的名称定义过程来简单地重写过程,但我遇到了一些错误,如
错误:位于(1)的“mandatoryprocedureollExtends”的伪参数“varInt”应命名为“varReal”,以匹配重写过程的相应参数


所以在这一点上,我甚至不确定是否有可能用fortran实现这一点。我不希望type1是抽象类型,但如果需要的话,也可以是抽象类型。

我已经更新了答案,以包含这两个函数的声明。基本上,我只需更改名称,并根据需要编写过程。我将此用作参考。在我看来,您缺少类型绑定过程定义中的
NOPASS
属性。除此之外,我看不出你尝试泛型有什么问题。也许你可以用泛型做一个完整的例子,以防万一有什么微妙的事情?实际上我没有使用NOPASS,我只是没有在问题的代码中写“this”变量,但现在我已经修复了它。谢天谢地,有些微妙的东西。在撰写具体案例时,正如你所问,我设法解决了这个问题。在使用模块的程序中,我使用错误的类型定义了一个变量作为泛型过程的输入参数。因此,编译器无法将我对过程的调用与定义的任何泛型调用相匹配。现在这个问题得到了解决,并按预期工作。谢谢你的帮助