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
Pointers Fortran过程指针作为参数。错误:位于(1)的过程参数无效_Pointers_Fortran_Procedure - Fatal编程技术网

Pointers Fortran过程指针作为参数。错误:位于(1)的过程参数无效

Pointers Fortran过程指针作为参数。错误:位于(1)的过程参数无效,pointers,fortran,procedure,Pointers,Fortran,Procedure,我试图通过使用指针将一个函数作为另一个函数的参数。我一直收到错误:错误:在(1)处的过程参数无效。。关于主程序中的参数f_ptr in diff。有人知道如何解决这个问题吗 module functions contains function f(x) result(y) double precision x, y y = sin(x) end function f end module MODULE numerical_math contains function dif

我试图通过使用指针将一个函数作为另一个函数的参数。我一直收到错误:
错误:在(1)处的过程参数无效。
。关于主程序中的参数f_ptr in diff。有人知道如何解决这个问题吗

module functions
contains

function f(x) result(y)
    double precision x, y
    y = sin(x)
end function f

end module

MODULE numerical_math
contains
function diff(f, x, degree) result(df)
    double precision, intent(in) :: x
    double precision :: h, df
    integer :: degree, i, j
    integer, allocatable, dimension(:) :: lst
    pointer :: f
    interface
        function f(x) result(y)
            double precision :: x, y
        end function f
    end interface

    ...

end function diff
END MODULE numerical_math

program main
    use numerical_math
    use functions
    implicit none
    integer :: first
    procedure (f), pointer :: f_ptr => null ()
    f_ptr => f
    print *, f_ptr(1.d0)

    do first = 1,9
        print *, diff(f_ptr, 0.d0, first)
    end do    
end program main

我想您的
diff
函数应该只接收一个函数作为参数,您不需要更改该函数(即,您不想更改目标函数)。 然后只需删除
diff
函数中的
指针::f
行:

function diff(f, x, degree) result(df)
    double precision, intent(in) :: x
    double precision :: h, df
    integer :: degree, i, j
    integer, allocatable, dimension(:) :: lst
    ! pointer :: f  REMOVE THIS LINE
    interface
        function f(x) result(y)
            double precision :: x, y
        end function f
    end interface

    ...

end function diff

我想您的
diff
函数应该只接收一个函数作为参数,您不需要更改该函数(即,您不想更改目标函数)。 然后只需删除
diff
函数中的
指针::f
行:

function diff(f, x, degree) result(df)
    double precision, intent(in) :: x
    double precision :: h, df
    integer :: degree, i, j
    integer, allocatable, dimension(:) :: lst
    ! pointer :: f  REMOVE THIS LINE
    interface
        function f(x) result(y)
            double precision :: x, y
        end function f
    end interface

    ...

end function diff

如果您遇到相同的问题,我通过使用Fortran 95编写项目来解决这个问题。在Fortran 95中,当您使用Jack的建议(删除
指针::f
行并将函数作为参数而不是指针)时,它会编译。我还没有在Fortran 90中找到这个问题的解决方案。

如果您遇到同样的问题,我会用Fortran 95来解决这个问题。在Fortran 95中,当您使用Jack的建议(删除
指针::f
行并将函数作为参数而不是指针)时,它会编译。我还没有在Fortran 90中找到解决问题的方法。

(不是OP):与中一样,类型声明是隐式的,因为接口语句?@tasospapstyllanou对于数据对象没有类型声明,而是接口声明。如果您需要pointer属性,那么您可以使用诸如
过程(f_接口)、pointer::f
之类的东西,在这里您在其他地方定义了一个
抽象接口f_接口(不是OP):如,类型声明是隐式的,因为接口语句?@TASOSPAPSTYLEANOU没有数据对象的类型声明,而是接口声明。如果您需要pointer属性,那么您可以使用诸如
过程(f_接口)、pointer::f
之类的东西,您可以在其他地方定义一个
抽象接口f_接口
。在传递函数时,您根本不需要函数指针。函数指针只是在Fortran 2003中添加的,用于更高级的东西,如存储函数地址并在以后更改它。您只需要正常的函数参数。@SamWoef您使用的是哪种编译器和版本?我在netbeans上使用的是mingw32 gcc fortran。如果在
main
diff
中调用
diff(f,0.d0,first)
会发生什么?仍然是相同的错误。为了清楚起见,我离开了接口,对吗?在传递函数时,您根本不需要函数指针。函数指针只是在Fortran 2003中添加的,用于更高级的东西,如存储函数地址并在以后更改它。您只需要正常的函数参数。@SamWoef您使用的是哪种编译器和版本?我在netbeans上使用的是mingw32 gcc fortran。如果在
main
diff
中调用
diff(f,0.d0,first)
会发生什么?仍然是相同的错误。说清楚,我离开了界面,对吗?