2017-04-06 99 views
0

我试图过程指针传递给一个派生类型结合-过程传递过程指针到派生类型结合-过程

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)。在所有其他情况下,虚拟参数会自动传递,为什么不在这里?作为过程使用的对象发生变化,我声明伪参数是不可行的。 有没有解决我的问题的方法?

+0

你能展示一个完整的程序,或至少显示所有相关的声明吗? – francescalus

+0

对不起,因为代码是保密的,所以不可能。你是否错过了声明的特定部分? – THo

+0

我们可以假设'type(mytype)mytypeobject'和'type(storedtype)storedtypeobject',但确认那会很好。 – francescalus

回答

0

那么你没有传递一个程序指针。 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,但如果您需要在某些情况下将其设置为其他目标。

+0

感谢您的回答。有意义的是,目前这不是一个指针,因为我从来没有把它定义为一个指针。我会研究这个! – THo

+0

注意:对于普通的指针,现在可以将一个'target'变量存入'pointer,intent(in)'中。不过,我不知道类似的过程指针的可能性。纠正我,如果我错了。 –

+0

@VladimirF你错了。 Fortran 2008允许将过程传递给有一些限制的过程指针(F2008 12.5.2.9p5)。我希望看到一个实际的测试案例,而不是一个不正确的片段。但可能有问题的编译器(可能是ifort)在正在使用的版本中不支持该编译器。 –