fortran模块出错

时间:2015-05-13 00:31:28

标签: fortran

我创建了一个带有子程序和主程序的fortran模块来调用该子程序。但是我遇到了一些我无法解决的错误。谁有人评论?这是代码:

module test
contains
subroutine sub1(array1,a) 
implicit none
real, intent(in) :: array1
integer, intent (in) :: a
integer::temp =1
array1 = (/ (temp, temp=1,a) )
write (*,'(I0)') array1
write (*,*) 'Number of columns = ',a
end subroutine
end module


program p1
use test
implicit none
real,allocatable :: array1 (:,:)
integer :: a =5
allocate (array1(1,a))
call sub1(array1,a)
end program

我收到以下错误:

array1 = (/ (temp, temp=1,a) )
Error: Syntax error in array constructor at (1)

Fatal Error: Can't open module file 'test.mod' No such file or directory

我试着找到一些方法,但无法弄清楚出了什么问题。再次,赞赏评论或帮助。

3 个答案:

答案 0 :(得分:3)

我看到了一些问题,其他人指出了一些问题:

  • 您已将array1传递给具有意图in的子例程,但您更改了它。使用intent(out)intent(inout)(如果您在其他位置初始化部分内容)
  • array1p1中的二维数组,但在sub1中声明为标量值。在array1(:,:)的声明中再次使用sub1。或者,您可以将其声明为array1(1,a)
  • 您正在使用的数组构造函数需要更改:
    • 正如其他人所指出的,您需要/)来终止构造函数
    • array1是一个2D数组,您正在尝试将1D向量(从1到a的系列)分配给它。由于array1的第一个维度的大小为1,因此您可以使用array1(1,:) =,这将允许从一维数组进行分配。
    • (更小的一点)temp是一个整数,而array1是一个整数。编译器会自动从整数转换为实数,但很好理解正在发生的事情。因此,我建议您使用real(temp)作为构造函数。
  • 您在array1中将sub1写为单个整数(I0);这是一个2D真实阵列。对{write}语句使用write(*,*)将起作用。
  • 您无需在temp中将sub1初始化为1(感谢@PetrH)。

编辑后的代码是:

module test
contains
subroutine sub1(array1,a)
implicit none
real, intent(inout) :: array1(:,:)
integer, intent (in) :: a
integer::temp =1
array1(1,:) = (/ (real(temp), temp=1,a) /)
write (*,*) array1
write (*,*) 'Number of columns = ',a
end subroutine
end module


program p1
use test
implicit none
real,allocatable :: array1 (:,:)
integer :: a =5
allocate (array1(1,a))
call sub1(array1,a)
end program

用PGI为我编译,给出:

1.000000        2.000000        3.000000        4.000000
5.000000
Number of columns =             5

其他一些想法:

  • 模块通常用自己的文件编写。但是,如果模块出现在程序语句之前(正如您在此处所做的那样),它将起作用。
  • 为什么宣布array1为二维但将第一维设置为大小为1?
  • 您可以使用array1轻松访问a,而不是将sub1的第二个维度的大小作为size(array1)传递给{{1}}。 (感谢@petrH)

答案 1 :(得分:0)

根据我在google上找到的一些array constructors文档,以下行不正确:

array1 = (/ (temp, temp=1,a) )

,应该是

array1 = (/ (temp, temp=1,a) /)

答案 2 :(得分:0)

您必须将模块放在单独的文件test.f90中。 此外,您将收到一个错误,即您在变量定义上下文中使用了INTENT(IN)的Dummy参数。

我认为这是因为您正在更改输入数组。尝试分配像array2 = array1

这样的虚拟对象

最后但并非最不重要的是array2=(/ (temp,temp=1,a) /)行(必须像Greg所说的那样更改为此)表示排名为2的数组,array1只是排名为1的数组。