具有假定形状的虚拟参数的过程必须具有显式接口 [英] Procedure with assumed-shape dummy argument must have an explicit interface

查看:13
本文介绍了具有假定形状的虚拟参数的过程必须具有显式接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Fortran 90 完全陌生,我正在尝试了解如何将数组传递给函数.我在网上看了看,找不到任何清晰和简单的例子,所以我决定在这里发布.

我希望函数能够处理任意长度的数组(数组的长度不应该是函数的参数之一).

我尝试编写一个简单的函数示例,该函数返回数组元素的总和:

函数 mysum(arr)隐式无真实的,维度(:),意图(在)::arr真实的 :: mysum整数 :: i,arrsizearrsize = 大小(arr)我的总和=0.0做 i=1,arrsizemysum=mysum+arr(i)结束结束函数 mysum程序测试隐式无真实的,维度(4)::一个真实 :: mysum,a_sum调用 random_number(a)打印 *,aa_sum=mysum(a)打印 *,a_sum结束程序

当我尝试编译时,我收到以下错误:

array_test.f90:17.14:真正的 mysum,a_sum1错误:(1)处的过程mysum"具有假定形状的虚拟参数arr"必须具有显式接口

我的程序有什么问题?

解决方案

假定形状的虚拟参数(那些带有 (:) 的)需要 显式接口可在呼叫站点获得.这意味着调用代码必须知道子例程标题的确切外观.另请参阅使用隐式接口调用外部过程的模块p>

可以通过多种方式提供显式接口

1.首选 - 一个模块过程

模块过程隐式无包含函数 mysum(arr)真实的,维度(:),意图(在)::arr真实的 :: mysum整数 :: i,arrsizearrsize = 大小(arr)我的总和=0.0做 i=1,arrsizemysum=mysum+arr(i)结束结束函数 mysum端模块程序测试使用程序隐式无!no mysum 这里声明,它来自模块...结束程序

2.内部过程 - 仅适用于简短的简单过程或如果该过程需要访问主机的变量.由于访问主机变量,它很容易出错.

程序测试隐式无!no a_sum 在这里声明,它在下面可见包含...包含函数 mysum(arr)!implicit none 从程序继承真实的,维度(:),意图(在)::arr真实的 :: mysum整数 :: i,arrsizearrsize = 大小(arr)我的总和=0.0做 i=1,arrsizemysum=mysum+arr(i)结束结束函数 mysum结束程序

3.界面块 - 完全不推荐,你应该有一些特殊的理由来使用它

函数 mysum(arr)!删除以节省空间结束函数 mysum程序测试隐式无界面函数 mysum(arr)真实的,维度(:),意图(在)::arr真实的 :: mysum结束函数端接口!no mysum 在那里声明!它在接口块中声明...结束程序

I am completely new to Fortran 90 and I am trying to understand how to pass an array to a function. I looked on the web and I could not find any clear and simple enough example, so I decided to post here.

I would like the function be able to work on an array of any length (the length of the array should not be one of the parameters of the functions).

I tried to write a simple example of a function that returns the sum of the elements of an array:

function mysum(arr)
    implicit none
    real, dimension(:), intent(in) :: arr
    real :: mysum
    integer :: i,arrsize
    arrsize = size(arr)
    mysum=0.0
    do i=1,arrsize
        mysum=mysum+arr(i)
    enddo
end function mysum

program test
    implicit none
    real, dimension(4) :: a
    real :: mysum,a_sum
    call random_number(a)
    print *,a
    a_sum=mysum(a)
    print *,a_sum
end program

When I try to compile, I get the following error:

array_test.f90:17.14:

 real mysum,a_sum
           1
Error: Procedure 'mysum' at (1) with assumed-shape dummy argument 'arr' must have an explicit interface

What is the problem with my program?

解决方案

Assumed shape dummy arguments (those with (:)) require explicit interface to the procedure to be available at the call site. That means the calling code must know how exactly the subroutine header looks like. See also Module calling an external procedure with implicit interface

That explicit interface can be provided in several ways

1. preferred - a module procedure

module procedures
  implicit none

contains

  function mysum(arr)

    real, dimension(:), intent(in) :: arr
    real :: mysum
    integer :: i,arrsize
    arrsize = size(arr)
    mysum=0.0
    do i=1,arrsize
        mysum=mysum+arr(i)
    enddo
  end function mysum
end module

program test
    use procedures

    implicit none
    !no mysum declared here, it comes from the module
    ...
end program

2. internal procedure - only for short simple procedures or if the procedure needs access to the host's variables. Because of the access to the host variables it is error-prone.

program test
    implicit none
    !no a_sum declared here, it is visible below contains
    ...    
contains

  function mysum(arr)

    !implicit none inherited from the program

    real, dimension(:), intent(in) :: arr
    real :: mysum
    integer :: i,arrsize
    arrsize = size(arr)
    mysum=0.0
    do i=1,arrsize
        mysum=mysum+arr(i)
    enddo
  end function mysum
end program

3. interface block - not recommended at all, you should have some particular reason to use it

function mysum(arr)
  ! removed to save space
end function mysum

program test
    implicit none

     interface
       function mysum(arr)
         real, dimension(:), intent(in) :: arr
         real :: mysum
       end function
     end interface

     !no mysum declared there
     !it is declared in the interface block
     ...
end program

这篇关于具有假定形状的虚拟参数的过程必须具有显式接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆