Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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
Class 在Fortran中从外部类传递子例程名称_Class_Oop_Fortran_Gfortran_Subroutine - Fatal编程技术网

Class 在Fortran中从外部类传递子例程名称

Class 在Fortran中从外部类传递子例程名称,class,oop,fortran,gfortran,subroutine,Class,Oop,Fortran,Gfortran,Subroutine,在中,我们学习了如何在Fortran类中传递子例程名称作为参数。但是我们如何从外部类传递子例程名称呢 随后的代码使用GNU Fortran(GCC)5.1.0为两次不同的尝试生成编译错误: 例程选择器的目标是采用不同的计算路径:一个是将数字平方,另一个是将其加倍。第一个编译错误建议添加参数列表。对此天真的补救措施会产生第二个错误 随后是一个MWE。排列编程产生了许多变体;希望这个版本可以很容易地修复 module myModule implicit none type

在中,我们学习了如何在Fortran类中传递子例程名称作为参数。但是我们如何从外部类传递子例程名称呢

随后的代码使用GNU Fortran(GCC)5.1.0为两次不同的尝试生成编译错误:

例程
选择器的目标是采用不同的计算路径:一个是将数字平方,另一个是将其加倍。第一个编译错误建议添加参数列表。对此天真的补救措施会产生第二个错误

随后是一个MWE。排列编程产生了许多变体;希望这个版本可以很容易地修复

module myModule

    implicit none

    type     :: intermediates
        real :: z
    contains
        private
        procedure, nopass, public :: square => square_sub
        procedure, nopass, public :: double => double_sub
    end type intermediates

    private :: square_sub
    private :: double_sub

contains

    subroutine square_sub ( x, y )
        real, intent ( in )  :: x
        real, intent ( out ) :: y
            y = x ** 2
    end subroutine square_sub

    subroutine double_sub ( x, y )
        real, intent ( in )  :: x
        real, intent ( out ) :: y
            y = x * 2
    end subroutine double_sub

end module myModule


program casey

    use myModule
    implicit none

    real :: x = 10.0, y
    type ( intermediates ) :: ints
        call selector ( ints % square, x , y )
        call selector ( ints % double ( x, y ), x , y )

contains

    subroutine selector ( sub, x, y )

        interface mySub
            subroutine sub ( x, y )
                real, intent ( in )  :: x
                real, intent ( out ) :: y
            end subroutine sub
        end interface mySub

        real, intent ( in )  :: x
        real, intent ( out ) :: y

            call sub ( x, y )
            print *, 'x = ', x, ', y = ', y

    end subroutine selector

end program casey

解决方案是将选择器过程放置在类中。在上面的示例中,
子例程选择器
位于
程序
内。下面的
子程序本地\u选择器
是类型
mySubs
中的
过程

module mySubs

    implicit none

    type :: myClass
    contains
        procedure, nopass, public :: square
        procedure, nopass, public :: double
        procedure, nopass, public :: local_selector
    end type myClass

contains

    subroutine square ( x, y )
        real, intent ( in )  :: x
        real, intent ( out ) :: y
            y = x ** 2
            print *, 'x = ', x, '; x ** 2 = ', y
    end subroutine square

    subroutine double ( x, y )
        real, intent ( in )  :: x
        real, intent ( out ) :: y
            y = x * 2
            print *, 'x = ', x, '; 2 x = ', y
    end subroutine double

    subroutine local_selector ( sub, x, y )

        interface mySub
            subroutine sub ( x, y )
                real, intent ( in )  :: x
                real, intent ( out ) :: y
            end subroutine sub
        end interface mySub

        real, intent ( in )  :: x
        real, intent ( out ) :: y

            call sub ( x, y )

    end subroutine local_selector

end module mySubs

program fixed

    use mySubs
    implicit none

    real :: x = 10.0, y

    type ( myClass ) :: thisClass

        call thisClass % local_selector ( square, x , y )
        call thisClass % local_selector ( double, x , y )

end program fixed

我认为应该有另一个问题向你指出。相反,在短期内,
ints%square
不是一个过程,而是一个绑定名称。这可能足以让您继续搜索。请阅读您引用的操作“内部”和“外部”类(在Fortran中称为派生类型)-Fortran中的派生类型内部没有可执行操作。类型具有绑定和过程指针组件,这些绑定和组件引用的过程总是在派生类型定义的范围之外。
module mySubs

    implicit none

    type :: myClass
    contains
        procedure, nopass, public :: square
        procedure, nopass, public :: double
        procedure, nopass, public :: local_selector
    end type myClass

contains

    subroutine square ( x, y )
        real, intent ( in )  :: x
        real, intent ( out ) :: y
            y = x ** 2
            print *, 'x = ', x, '; x ** 2 = ', y
    end subroutine square

    subroutine double ( x, y )
        real, intent ( in )  :: x
        real, intent ( out ) :: y
            y = x * 2
            print *, 'x = ', x, '; 2 x = ', y
    end subroutine double

    subroutine local_selector ( sub, x, y )

        interface mySub
            subroutine sub ( x, y )
                real, intent ( in )  :: x
                real, intent ( out ) :: y
            end subroutine sub
        end interface mySub

        real, intent ( in )  :: x
        real, intent ( out ) :: y

            call sub ( x, y )

    end subroutine local_selector

end module mySubs

program fixed

    use mySubs
    implicit none

    real :: x = 10.0, y

    type ( myClass ) :: thisClass

        call thisClass % local_selector ( square, x , y )
        call thisClass % local_selector ( double, x , y )

end program fixed