将未定义大小的数组传递给子例程

时间:2017-11-23 11:59:33

标签: fortran gfortran

我试图将一个未指定大小的数组传递给像这样的子程序

PROGRAM GOL
IMPLICIT NONE

INTEGER, PARAMETER :: size_x = 16, size_y = 16
LOGICAL, DIMENSION(1:size_x,1:size_y) :: universe
universe(:,:) = .FALSE.

CALL COUNT_NEIGHBOURS(universe, 1, 1)

END PROGRAM GOL

SUBROUTINE COUNT_NEIGHBOURS (universe, x, y)
LOGICAL, DIMENSION(:,:) :: universe
INTEGER :: x,y

!test
universe(x,y) = .TRUE.

RETURN
END SUBROUTINE COUNT_NEIGHBOURS

但是我从gfortran

得到了错误
CALL COUNT_NEIGHBOURS(universe, 1, 1)
                     1
Error: Procedure 'count_neighbours' at (1) with assumed-shape dummy argument 'universe' must have an explicit interface

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

如错误消息中所述,编译器需要可调用过程的显式接口。 This answer显示了如何提供界面的三种方法。

在大多数情况下,将程序放入模块或使其成为一个包含程序的首选方法,但有时它更容易使用接口块(在下面的代码中),例如,您需要更新一些旧代码。

PROGRAM GOL
IMPLICIT NONE

interface
    subroutine COUNT_NEIGHBOURS(universe, x, y)
        logical :: universe(:,:)
        integer :: x, y
    end subroutine COUNT_NEIGHBOURS
end interface


INTEGER, PARAMETER :: size_x = 16, size_y = 16
LOGICAL, DIMENSION(1:size_x,1:size_y) :: universe
universe(:,:) = .FALSE.

CALL COUNT_NEIGHBOURS(universe, 1, 1)

END PROGRAM GOL

SUBROUTINE COUNT_NEIGHBOURS (universe, x, y)
LOGICAL :: universe(:,:)
INTEGER :: x,y

!test
universe(x,y) = .TRUE.

RETURN
END SUBROUTINE COUNT_NEIGHBOURS