【问题标题】:Passing procedure pointer to derived-type-bound-procedure将过程指针传递给派生类型绑定过程
【发布时间】:2023-03-22 02:25:01
【问题描述】:

我正在尝试将过程指针传递给派生类型绑定过程

module mymodule
use mystoremodule
    implicit none
    private
    type, abstract, public :: mytype
    contains
        procedure :: parse
    end type mytype
contains

subroutine parse(self,mypointer)
    implicit none

    ! Declaring Part
    class(mytype) :: self
    procedure(storing),pointer, intent(in) :: mypointer 
    integer :: myvalue

    ! Executing Part
    myvalue = 42
    call mypointer(myvalue)

end subroutine parse
end module mymodule

storing 在另一个模块/派生类型中定义

module mystoremodule
    implicit none
    type, public :: storingtype
    integer :: myvalue
    contains
        procedure, public :: storing
    end type storingtype
contains

subroutine storing(self,myvalue)
    ! Declaring part
    class(storingtype) :: self
    integer, intent(in) :: myvalue
    ! Executing part
    self%myvalue = myvalue
end subroutine  SetExcitationOrder
end module mystoremodule

我调用这个过程

call mytypeobject%parse(storingtypeobject%storing)

这样我得到一个编译器错误

The type of the actual argument differs from the type of the dummy argument.

我发现错误来自过程指针没有将虚拟参数传递给storing 过程(我没有将任何东西定义为nopass)。在所有其他情况下,虚拟参数会自动传递,为什么不在这里呢?声明虚拟参数对我来说是不可行的,因为过程使用的对象发生了变化。 我的问题有什么解决方案吗?

【问题讨论】:

  • 你能展示一个完整的程序,或者至少展示所有相关的声明吗?
  • 抱歉,因为代码是机密的,所以不可能。您是否遗漏了声明的特定部分?
  • 我们可能会假设type(mytype) mytypeobjecttype(storingtype) storingtypeobject 但确认会很好。
  • This question 也可能感兴趣。

标签: fortran fortran2003


【解决方案1】:

嗯,你没有传递一个过程指针。 storingtypeobject%storing 不是过程指针,它是对类型绑定过程的绑定,而不是任何类型的指针。

我实际上不会接受指针,而只是parse 中的过程参数。

类似:

subroutine parse(self,mypointer)
    implicit none

    ! Declaring Part
    class(mytype) :: self
    procedure(storing), intent(in) :: myproc
    integer :: myvalue

    ! Executing Part
    myvalue = 42
    call myproc(myvalue)

end subroutine parse

    call mytypeobject%parse(storing_wrapper)

  contains

    subroutine storring_wrapper(myvalue)
      integer, intent(in) :: myvalue
      call storingtypeobject%storing(myvalue)
    end subroutine

我认为过程指针只有在可以在某处更改时才最有用。不一定在parse 中,但如果您需要在某些情况下将其设置为其他目标。

【讨论】:

  • 感谢您的回答。这是有道理的,这不是当前的指针,因为我从来没有将它定义为指针。我会调查的!
  • 注意:对于普通指针,现在可以将target 变量传递给pointer, intent(in)。但是我不知道过程指针有类似的可能性。如果我错了,请纠正我。
  • @VladimirF 你错了。 Fortran 2008 允许将过程传递给具有一些限制的过程指针 (F2008 12.5.2.9p5)。我希望看到一个实际的测试用例,而不是一个不正确的 sn-p。但可能是有问题的编译器(可能是 ifort)在正在使用的版本中不支持它。
  • @SteveLionel 即使是与类型指示符一起使用的绑定?
  • 我做了一些测试,我仍然认为答案是正确的。只有我上面的第一条评论是不正确的。
猜你喜欢
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多