问题
I am trying to pass an array of unspecified size to a subroutine like so
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
However I get the error from 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
What is the correct way to do this?
回答1:
As described in the error message the compiler needs the explicit interface for your callable procedure. This answer shows three ways how to provide an interface.
In most cases the preferred way to put the procedure in a module or make it a contained procedure, but sometimes it's easier to use the interface block (in the code below) when, for example, you need to update some old code.
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
来源:https://stackoverflow.com/questions/47455067/passing-an-array-of-undefined-size-to-a-subroutine