FORTRAN 95,数组必须具有恒定的形状误差 [英] FORTRAN 95, array must have constant shape error
问题描述
我是一个绝对的初学者Fortran95,所以我对这个问题的简单道歉。
我希望能够在整数来定义数组的尺寸,但我得到的错误
模块或主程序数组'arrayx在(1)必须有常量外形
和'变量'Y'不能出现在:(1)前pression'(对于x同样的错误)。
它是根本不可能的变量来定义尺寸还是我只是缺少明显的东西。
程序arrayForm
隐无
整数,节省:: X = 40,Y = 2
真实的,尺寸(X,Y):: arrayX
程序结束arrayForm
与变量保存
属性不是常量和编译器的理由抱怨。在你的代码片段替换为参数
属性,这使得他们不断的和,在编译时该属性,该阵列可以固定在规模和编译器不应该抱怨。
正如你所采用它保存
属性是多余的。当施加到变量可能超出范围,如一个子例程或函数的范畴内的变量,它要求该变量的值保存从子范围中的一个调用到下一个。但由于你的变量在程序范围内声明,他们永远不会走出去的范围执行过程中。
请注意也Fortran语言的陷阱新(老)的程序员,一个是,这是在他们的声明中初始化变量获得自动保存
属性。所以,在一个子程序你行
整数,节省:: X = 40,Y = 2
可以简单地写
整数:: X = 40,Y = 2
我个人倾向于让自己的意图,当我写一个子程序都清楚,我以后使用保存
属性。
I'm an absolute beginner to Fortran95 so I apologise for the simplicity of this question. I want to be able define the dimensions of an array in terms of integers but I get the error
'The module or main program array 'arrayx' at (1) must have constant shape'
and 'Variable 'y' cannot appear in the expression at (1)' (same error for x). Is it simply not possible to define the dimensions in terms of variables or am I just missing something obvious.
program arrayForm
implicit none
integer, save :: x=40, y=2
real, dimension(x,y) :: arrayX
end program arrayForm
Variables with the save
attribute are not constants and the compiler complains with justification. In your snippet replace that attribute with the parameter
attribute which makes them constant and, at compile-time, the array can be fixed in size and the compiler shouldn't complain.
As you have employed it the save
attribute is redundant. When applied to variables which may go out of scope, such as variables inside the scope of a subroutine or function, it requires that the value of the variable is saved from one invocation of the sub-scope to the next. But since your variables are declared in your program scope they won't ever go out of scope during execution.
Note also that one of Fortran's gotchas for new (and old) programmers is that variables which are initialised in their declaration acquire the save
attribute automatically. So, in a subroutine your line
integer, save :: x=40, y=2
could simply be written
integer :: x=40, y=2
Personally I tend to use the save
attribute so that my intentions when I write a subroutine are clear to me later.
这篇关于FORTRAN 95,数组必须具有恒定的形状误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!